LoginSignup
1
3

More than 5 years have passed since last update.

TeX環境作成

Last updated at Posted at 2018-12-15

MacにTeX環境作ってみる

texのinstallまわり

brewをsearchすると

If you meant "tex" specifically:
Installing TeX from source is weird and gross, requires a lot of patches,
and only builds 32-bit (and thus can't use Homebrew dependencies)

We recommend using a MacTeX distribution: https://www.tug.org/mactex/

You can install it with Homebrew-Cask:
  brew cask install mactex

と出るのでその通りMacTeXを使ってみる。

以下を参考にして
* http://www.clas.kitasato-u.ac.jp/~fujiwara/MacTeX/
* http://doratex.hatenablog.jp/entry/20180501/1525144736#13

TeXShopの環境設定で、プロファイルとしてupTeX (ptex2pdf)を選択して保存・再起動する。

インストール先などは/Library/TeXらしい。

VSCodeの場合は LaTeX workshop という拡張がある。

markdownとの連携

コード断片など、アンダースコアなどが入る文書はいちいち手でエスケープするのが面倒なので、markdownでマークアップしてpandocで変換してTeXソースにするのが良さそう。

しかし変換してみると、コード領域などが

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{\{}
  \DataTypeTok{"premiumId"}\FunctionTok{:} \StringTok{"13"}\FunctionTok{,}
  \DataTypeTok{"text"}\FunctionTok{:} \StringTok{""}\FunctionTok{,}
  \DataTypeTok{"context"}\FunctionTok{:} \FunctionTok{\{\}}
\FunctionTok{\}}
\end{Highlighting}
\end{Shaded}

などといった未知の環境で生成されている。

セクション周りも

\hypertarget{chat_trans}{%
\subsubsection{\texorpdfstring{\texttt{/chat\_trans}}{/chat\_trans}}\label{chat_trans}}

と非標準コマンドであった。

これらについては、

pandoc -s ドキュメント作業メモ.md -o DocMemo1.tex

としてスタンドアローンモードで出力させると、その辺のコマンドも含んだプリアンブルを出力しているので、それを切り出してpandoc_pre.texなどとしてインクルードファイルにして使いまわせば良さそう。
ただし一件だけ、

\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts

がエラーを出していたのでコメントアウトする。

作業手順

本文はmarkdownで書いて、pandocで変換する。pandoc変換後の.texのsubsectionをchapterに、subsubsectionをsectionに(適宜)直してゆく。
この辺、手修正が毎回入りそうなので、

A.md ->(pandoc)-> A_1.tex ->(copy/edit)-> A.tex

と3つファイルが存在するようにしておいて、A.mdを修正->A_1.texを生成->diff -u A_1.tex A.texを見ながらA.texを修正。A.texはmain.tex的なものからインクルードする。

上で作ったpandoc_pre.texで

\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}

となっていてインデントが殺されているので、まるっと不許可にしておく。

またコメントどおりに

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\small}

と修正。

フォントは

\usepackage{newtxtext,newtxmath}

を使う(pandoc_preのincludeより前で指定)。

markdownで画像を含めておいてpandoc変換すると

\begin{figure}
\centering
\includegraphics{figure_1.png}
\caption{構成図1}
\end{figure}

となるがこれは texコンパイル時にbounding boxがないのでエラーになる。
コマンドラインで

/Library/TeX/texbin/extractbb figure_1.png

して生成されたfigure_1.xbbを確認すると

%%Title: figure_1.png
%%Creator: extractbb 20180217
%%BoundingBox: 0 0 641 394
%%HiResBoundingBox: 0.000000 0.000000 641.000000 394.000000
%%CreationDate: Wed Sep 12 16:50:32 2018

となっていて、このHiResBoundingBox の値を

\includegraphics[bb=0.000000 0.000000 641.000000 394.000000]{figure_1.png}

のようにtexソース内に移植する。

その他 tex の動作を理解

TeXShopからタイプセットボタンでコンパイルできているので何が動いているのかよくわからないが

  1. /Library/TeX/texbin/ にもろもろコマンドがあり、
  2. どうもplatex, uplatex でコンパイル可能で(つまりマクロ pLaTeX を利用している)
  3. env PATH=/Library/TeX/texbin/:${PATH} ptex2pdf -l report1809.tex でもコンパイルできる

TeXShop 設定ではタイプセット設定で、デフォルトのスクリプトとして TeX + DVI を選んであり(多分設定プロファイルの (upTeX pTeX2pdf))、内部設定で TeX+dvipdfmx 云々で LaTeX 用に ptex2pdf -l 云々となっている。LaTeX が選ばれるのはタイプセットのデフォルトコマンドが LaTeX だから?

VScode 連携

VScode latex-workshopの設定

以下を参考にした。
http://haruka0000.hatenablog.com/entry/2018/07/20/190853

「レシピ」でptex2pdf と言う名前で ptex2pdf というツールを登録する。
「ツール」でこのptex2pdfという名前で、コマンドが ptex2pdf 、引数が云々と登録する。

  "latex-workshop.latex.recipes": [
    {
      "name": "latexmk",
      "tools": [
        "latexmk"
      ]
    },
    {
      "name": "ptex2pdf",
      "tools": [
        "ptex2pdf"
      ]
    },
    {
      "name": "pdflatex -> bibtex -> pdflatex*2",
      "tools": [
        "pdflatex",
        "bibtex",
        "pdflatex",
        "pdflatex"
      ]
    }
  ],
  "latex-workshop.latex.tools": [
    {
      "name": "latexmk",
      "command": "latexmk",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "%DOC%"
      ]
    },
    {
      "name": "pdflatex",
      "command": "pdflatex",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "%DOC%"
      ]
    },
    {
      "name": "bibtex",
      "command": "bibtex",
      "args": [
        "%DOCFILE%"
      ]
    },
    {
      "name": "ptex2pdf",
      "command": "ptex2pdf",
      "args": [
        "-l",
        "-ot",
        "-kanji=utf8 -synctex=1",
        "%DOC%"
      ]
    }
  ]

設定から
Latex-workshop › Latex: RecipesLatex-workshop › Latex: Tools
Edit insettings.jsonで登録する。

COMMANDSBuild LaTeX projexctのメニューにRecipe: ptex2pdfとして登場する。

ただしこの設定で、以下のようにptex2pdf は実行されて .dvi は作成されているっぽいが、 dvipdfmx に失敗している。


LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

 )
(see the transcript file for additional information)
Output written on report1809.dvi (27 pages, 115240 bytes).
SyncTeX written on report1809.synctex.gz.
Transcript written on report1809.log.
work.dvi -> work.pdf

dvipdfmx:fatal: Could not open specified DVI (or XDV) file: work.dvi

Output file removed.
ptex2pdf processing of /Users/ssuzuki/work.2018/08_adam1/docs/report1809 failed.

以下のような shell script を作ってptex2pdfの代わりに実行することで無理やり解決した。


#!/bin/sh -x

#pwd
#ls
#N=$#
DOC=`basename ${!#}`
ptex2pdf -l -ot -kanji=utf8 -synctex=1 $DOC

終了。

付記

TeX に upLaTeX を使う場合、 ptex2pdf のオプションに -u も追加する。

付記2

latex-workshop.latex.recipes で latexmk が先だからか、 .texファイル保存時に autobuild が走る時に latexmk を使おうとしてエラーになる(編集画面の波線もこれを利用している?)っぽい。
ptex2pdf を先にしたらエラーにならなくなった。

1
3
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
1
3