0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Markdown Preview Enhanced で Tikz が動かなかった

Posted at

TL;DR

  • Markdown Preview Enhancedでコードチャンクを使うと、tikzなどの図を表示することができる。
  • Windowsならpdf2svg+texliveをインストールし、PATHを通す(再起動も必要だった)。1
  • 日本語を含まないパスにする。
  • latex {cmd=true}latex_zoom=xxlatex_width=xxlatex_height=xxを追加する。

概要

Markdown Preview Enhanced は、VSCode で Markdown ファイルをプレビューするすごい拡張機能です。

マークダウンの文中で

$$
\begin{equation}
\frac{d}{dt}\left(\frac{\partial L}{\partial \dot{q}_i}\right) - \frac{\partial L}{\partial q_i} = 0
\end{equation}
$$

のように書くと、以下のように表示できます。
$$
\begin{equation}
\frac{d}{dt}\left(\frac{\partial L}{\partial \dot{q}_i}\right) - \frac{\partial L}{\partial q_i} = 0
\end{equation}
$$

しかし、この状態ではmathjaxのみが動くため、tikzなどの図を表示することができません。

そこで、Markdown Preview Enhancedの設定から、markdown-preview-enhanced.enableScriptExecution": trueとして次のコードチャンクを追加します。

> ```latex {cmd=true}
> \documentclass{standalone}
> \begin{document}
>   Hello world!
> \end{document}
> ```

(ただし、左端の>は不要です。)

するとプレビュー画面でコードにマウスを持っていくと実行ボタンが現れ、しばらく待つとTexのコードが画像に変換されて表示されるはずです。

しかし、うまくいきませんでした。

エラーの解決

まず、次のようなエラーが出ました。

'pdf2svg' �́A�����R�}���h�܂��͊O���R�}���h�A
����”\�ȃv���O�����܂��̓o�b�` �t�@�C���Ƃ��ĔF������Ă��܂���B

これはおそらくpdf2svgが存在しないことを表しています。
ここを参考にして、pdf2svgをインストールしPATHを通してください。(PATHを通したあとパソコンの再起動をしないと反映されませんでした。(なぜ?))

次に、このようなエラーが出ました。

Unable to open file

この場合、マークダウンファイルのパスに日本語が含まれている可能性があります。日本語を含まないパスに変更してください。

次に、このようなエラーが出ました。

EntryNotFound (FileSystemError): Error: ENOENT: no such file or directory, open <filepath>

この場合、latex {cmd=true}latex_zoom=xxlatex_width=xxlatex_height=xxを追加してください。1

これで晴れて、図を表示することができます。

>  ```latex {cmd=true, latex_zoom=300%, hide=true}
>  \documentclass{standalone}
>  \usepackage{tikz}
>  \begin{document}
>    \begin{tikzpicture}[samples=100]
>      \draw[->,>=stealth,semithick] (-0.3,0)--(2,0) node[right]{$x$};
>      \draw[->,>=stealth,semithick] (0,-0.3)--(0,3) node[above]{$y$};
>      \draw (0,0) node[below left]{O};
>
>      % 過剰和を赤色、不足和を青色で示す
>      \foreach \x in {0.0, 0.2, ..., 1.6}
>        \filldraw[fill=red!20] (\x,0) rectangle (\x+0.2,{max(\x*\x,(\x+0.2)*(\x+0.2))});
>        
>      \foreach \x in {0.0, 0.2, ..., 1.6}
>        \filldraw[fill=blue!20] (\x,0) rectangle (\x+0.2,{min(\x*\x,(\x+0.2)*(\x+0.2))});
>
>      \draw[domain=-0.3:1.85] plot(\x,{\x*\x});
>    \end{tikzpicture}
>  \end{document}
>  ```

のように書くと、次のように表示されます。

3e5fb921af0fde5e584c517699df029f_1.png

  1. https://github.com/shd101wyy/vscode-markdown-preview-enhanced/issues/1180 2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?