The One with ...

思いついたことや作業メモなどを公開しています

Mathematica+TikZ

タイトル通り,MathematicaでつくったグラフをTikZでお化粧してTeXに取り込む方法のメモです.

まず素材となるグラフをMathematicaでちゃっとつくります.

Plot[PDF[LogNormalDistribution[0, 1], x], {x, 0, 5}, 
 PlotStyle -> {Black}, Frame -> True,
 FrameTicks -> {{{0, 0.2, 0.4, 0.6}, None}, {{0, 1, 2, 3, 4, 5}, 
    None}}]

このグラフにlogn_pdfという名前をつけて保存しておきます.

次にTeXのFigure環境の中にTiKZでグリッドを描きます.\includegraphicsで先ほどつくったlogn_pdfを 読み込むのですが,tikzpicture環境の中で読み込むのがポイントです.

\begin{figure}[H]
    \centering
\begin{tikzpicture}
\draw[step=1,black!0] (0,0) grid (12,5);
\node[anchor=south west] (image) at (1.5,-0.5)
    {\includegraphics[width=0.7\linewidth]{logn_pdf}};
\node [right] at (4.432,3.5) 
{$\displaystyle \frac{1}{\sqrt{2 \pi \sigma^2} x}\exp \left\lbrace -\frac{(\log x-\mu)^2}{2\sigma^2}\right\rbrace$};
\node [right] at (0.7,4.8){$f(x)$};
\node [right] at (10,0){$x$}; 
\end{tikzpicture}
{\small 対数正規分布の確率密度関数$f(x)$のグラフ. $\mu=0,\sigma=1$}
\end{figure}

できあがりー.

f:id:hamada7418:20181018230630p:plain

グラフの中と周りにTeXの数式がきちんと挿入されました. R MrakdownJupyter notebookでも同様のことができます. ぜひお試しを.