Leun Kim's Blog風の強いお台場

その他 / ブログのコツ

[プラグイン] ブログにQuickLaTeXを導入(TikZでグラフや図を描く)

ブログのコツその他コメント2

韓国語で書いた記事を翻訳しました。 原文を読む →

ブログにQuickLaTeXを導入しました。QuickLaTeXの一番の魅力は、TikZのコードを画像としてレンダリングしてくれることです。もちろん普通の数式にも対応していますが、数式はすでにMathJaxで表示しているので、グラフや図を描くときだけ使うことになりそうです(とはいえ、グラフを描く機会はそれほどなさそうですが)。

QuickLaTeXプラグインのダウンロード:http://www.holoborodko.com/pavel/quicklatex

以下、QuickLaTeXを使った簡単な例を紹介します。コードがそのままレンダリングされてしまうので、begin の前の \ は省いています。

begin{tikzpicture}
[+preamble]
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
[/preamble]
\begin{axis}
\addplot3[surf,domain=0:360,samples=60] {cos(x)*cos(y)};
\end{axis}
\end{tikzpicture}

と入力すると、次のようにレンダリングされます。

\begin{tikzpicture} [+preamble] \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} [/preamble] \begin{axis} \addplot3[surf,domain=0:360,samples=60] {cos(x)*cos(y)}; \end{axis} \end{tikzpicture}

f(x,y) = \cos x \cos y

レンダリングに時間がかかるようなら、次のようにサンプル数を減らすこともできます。

begin{tikzpicture}
[+preamble]
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
[/preamble]
\begin{axis}
\addplot3[surf,domain=0:360,samples=20] {sin(x)*sin(y)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture} [+preamble] \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} [/preamble] \begin{axis} \addplot3[surf,domain=0:360,samples=20] {sin(x)*sin(y)}; \end{axis} \end{tikzpicture}
f(x,y) = \sin x \sin y

ほかにも、こんな図をいろいろとWeb上で直接描けます。

begin{tikzpicture}
[+preamble]
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
[/preamble]
\begin{axis}[xlabel=Time (weeks),ylabel=Individuals,
width=12cm,height=6cm
]
\addplot coordinates {(1,8) (2,9) (4,7) (5,13)
(6,12) (8,18) (9,17) (10,22) (11,41) (12,32)
(13,24) (14,21) (16,21.5) (21,19.4) (25,21.02)};
\node[coordinate,pin={below:{Control}}] at (axis cs:16,21.5) {};
\addplot coordinates {(1,4) (2,3) (4,5) (5,7)
(6,9) (7,8) (8,12) (9,23) (10,38) (11,34)
(12,35) (13,33.2) (14,27) (16,25) (21,24.4) (25,24.9)};
\node[coordinate,pin={above:{Treatment}}] at (axis cs:21,24.4) {};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture} [+preamble] \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} [/preamble] \begin{axis}[xlabel=Time (weeks),ylabel=Individuals, width=12cm,height=6cm ] \addplot coordinates {(1,8) (2,9) (4,7) (5,13) (6,12) (8,18) (9,17) (10,22) (11,41) (12,32) (13,24) (14,21) (16,21.5) (21,19.4) (25,21.02)}; \node[coordinate,pin={below:{Control}}] at (axis cs:16,21.5) {}; \addplot coordinates {(1,4) (2,3) (4,5) (5,7) (6,9) (7,8) (8,12) (9,23) (10,38) (11,34) (12,35) (13,33.2) (14,27) (16,25) (21,24.4) (25,24.9)}; \node[coordinate,pin={above:{Treatment}}] at (axis cs:21,24.4) {}; \end{axis} \end{tikzpicture}

 

begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
thick,main node/.style={circle,fill=red!20,draw,font=\sffamily\Large\bfseries}]
[+preamble]
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows}
[/preamble]
\node[main node] (1) {A};
\node[main node] (2) [below left of=1] {B};
\node[main node] (3) [below right of=2] {C};
\node[main node] (4) [below right of=1] {D};
\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {a} (4)
edge [bend right] node[left] {b} (2)
edge [loop above] node {c} (1)
(2) edge node [right] {d} (1)
edge node {0.1} (4)
edge [loop left] node {100} (2)
edge [bend right] node[left] {20} (3)
(3) edge node [right] {0.04} (2)
edge [bend right] node[right] {40} (4)
edge [loop below] node {1} (3)
(4) edge node [left] {e} (3)
edge [loop right] node {f} (4)
edge [bend right] node[right] {g} (1);
\end{tikzpicture}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm, thick,main node/.style={circle,fill=red!20,draw,font=\sffamily\Large\bfseries}] [+preamble] \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} \usetikzlibrary{arrows} [/preamble] \node[main node] (1) {A}; \node[main node] (2) [below left of=1] {B}; \node[main node] (3) [below right of=2] {C}; \node[main node] (4) [below right of=1] {D}; \path[every node/.style={font=\sffamily\small}] (1) edge node [left] {a} (4) edge [bend right] node[left] {b} (2) edge [loop above] node {c} (1) (2) edge node [right] {d} (1) edge node {0.1} (4) edge [loop left] node {100} (2) edge [bend right] node[left] {20} (3) (3) edge node [right] {0.04} (2) edge [bend right] node[right] {40} (4) edge [loop below] node {1} (3) (4) edge node [left] {e} (3) edge [loop right] node {f} (4) edge [bend right] node[right] {g} (1); \end{tikzpicture}

 

[+preamble]
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{gnuplot]
[/preamble]
begin{tikzpicture}[scale=2]
\shade[top color=red,bottom color=gray!50]
(0,0) parabola (1.5,2.25) |- (0,0);
\draw (1.05cm,2pt) node[above]
{$\displaystyle\int_0^{3/2} \!\!x^2\mathrm{d}x$};
\draw[style=help lines] (0,0) grid (3.9,3.9)
[step=0.25cm] (1,2) grid +(1,1);
\draw[->] (-0.2,0) -- (4,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,4) node[above] {$f(x)$};
\foreach \x/\xtext in {1/1, 1.5/1\frac{1}{2}, 2/2, 3/3}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};
\foreach \y/\ytext in {1/1, 2/2, 2.25/2\frac{1}{4}, 3/3}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};
\draw (-.5,.25) parabola bend (0,0) (2,4) node[below right] {$x^2$};
\end{tikzpicture}

[+preamble] \usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=newest} \usepackage{gnuplot] [/preamble] \begin{tikzpicture}[scale=2] \shade[top color=red,bottom color=gray!50] (0,0) parabola (1.5,2.25) |- (0,0); \draw (1.05cm,2pt) node[above] {$\displaystyle\int_0^{3/2} \!\!x^2\mathrm{d}x$}; \draw[style=help lines] (0,0) grid (3.9,3.9) [step=0.25cm] (1,2) grid +(1,1); \draw[->] (-0.2,0) -- (4,0) node[right] {$x$}; \draw[->] (0,-0.2) -- (0,4) node[above] {$f(x)$}; \foreach \x/\xtext in {1/1, 1.5/1\frac{1}{2}, 2/2, 3/3} \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$}; \foreach \y/\ytext in {1/1, 2/2, 2.25/2\frac{1}{4}, 3/3} \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$}; \draw (-.5,.25) parabola bend (0,0) (2,4) node[below right] {$x^2$}; \end{tikzpicture}

ほかにもさまざまな例と出力結果を、http://www.texample.net/tikz/examples/all などで見ることができます。

#QuickLatex#Tikz

このブログのブログのコツの記事

すべて見る

コメント 2

  1. 파도소리

    안녕하세요. 지나가다 좋은 포스트를 읽게 되었네요.글에서는 QuickLaTex를 블로그에 적용시키셨다고 하셨는데 지금 수식을 보니 MathJax를 쓰고 계시더라구요. 다시 MathJax로 바꾸신 이유가 있을까요?

    1. Leun Kim

      안녕하세요. 사실 Quick LaTeX를 제거한 별다른 이유는 없어요. 모바일 환경에서 Quick LaTeX보다 MathJax가 조금 더 컴패티블하다고 판단해서 바꿨네요. Quick LaTeX같은 경우 모바일에서 긴 수식을 볼 때 그림 파일이라 짤리던지 아니면 그림 전체가 축소되어 폰트 크기가 들쭉날쭉 하게 되는데 반해 MathJax의 경우 적어도 그런 문제는 없어서 바꿨습니다.

    コメントを書く

    名前とコメントはこのページに公開されます(非公開コメントはブログの持ち主だけが見られます)。メールアドレスは公開されません。

    ページの先頭へ