VSCodeで書いた論文などのTeXをGrammarlyにコピペする時などに便利です。
拡張機能(Tex2Txt)へのリンク
(Overleaf Workshop + GitHub Copilotとかいいですよね。参考)
目次
- 使い方
- 実例
- 設定方法
使い方
変換したい.texファイルを開き、コマンドパレットからTex2Txt: Convert tex to txt
を実行してください。
変換後の文字列が表示されます。
実例
これが、、
\documentclass{article}
\begin{document}
% This is a comment
This content should remain.
\iffalse
This content should be removed.
\fi
\if 0
This content should also be removed.
\fi
\begin{comment}
This block should also be removed.
\end{comment}
\section{Introduction}
This is the introduction.
eq: $A formula$
a paper~\cite{hoge,fuga}
\begin{figure}
\begin{minipage}
\centering
\includegraphics[width=0.5\textwidth]{example.png}
\subcaption{This is a subcaption.}
\end{minipage}
\begin{minipage}
\centering
\includegraphics[width=0.5\textwidth]{example.png}
\subcaption{This is a subcaption 2.}
\end{minipage}
\caption{This is a figure $hoge fuga eq$ caption}
\label{fig:example}
Some figure content that should be removed.
\end{figure}
\end{document}
こうなります。
\documentclass{article}
This content should remain.
Introduction.
This is the introduction.
eq: [FORMULA]
a paper[CITATION]
This is a figure [FORMULA] caption.
This is a subcaption.
This is a subcaption 2.
設定方法
消したいブロックの設定
コメントなど削除したいブロックの開始と終了部分を設定します。
以下のように設定すると、\begin{comment} ... \end{comment}
やif 0 ... fi
が削除されます。
正規表現で記述可能です。
なお、勝手にコメント(% 以降
)は消します。
"texToTxtConverter.removableSurroundings": [
[
"\\\\begin{comment}",
"\\\\end{comment}"
],
[
"\\\\if\\s*0",
"\\\\fi"
]
]
TeXコマンドは削除しつつその内容は保持するための設定
\section{"section title"}
のsection title
のみを抽出するための設定です。
以下に列挙したコマンドは削除しつつその内容を保持します。
(ついでに末尾がピリオドでなければピリオドを付与します。
"texToTxtConverter.contentExtractionCommands": [
"section",
"subsection",
"subsubsection",
"footnote",
]
TeXコマンドを特定の文字列に置き換えるための設定
\cite{p1,p2}
を[CITATION]
などに置き換えます。
以下のように辞書形式でコマンド名
と置き換え先の文字列
を設定します。
置き換え先を""
とすれば削除と同等に動くはずです。
"texToTxtConverter.latexCommandReplacements": {
"cite": "[CITATION]",
"ref": "[REFERENCE]",
"label": "[LABEL]",
"eqref": "[EQUATION]",
"url": "[URL]"
}
TeX環境は削除しつつその内容は保持するための設定
\begin{abstract} hoge fuga piyo \end{abstract}
のhoge fuga piyo
のみを保持するための設定です。
以下のように保持したい環境を列挙してください。
\begin{x}\end{x}
は削除しつつその内容は保持します。
"texToTxtConverter.contentExtractionEnvironments": [
"document",
"abstract",
"itemize",
"enumerate",
]
TeX環境は削除しつつ、環境内の特定コマンドの内容は保持するための設定
図表などのTeX記述は削除しつつ、キャプションは保持するなどに利用できます。
\begin{figure}...\caption{hello world} ... \end{figure}
のhello world
だけは保持できるようになります。
removableEnvironments
に削除したい環境を、captionCommands
に保持したいコマンドを記入してください。
"texToTxtConverter.captionCommands": [
"caption",
"subcaption"
],
"texToTxtConverter.removableEnvironments": [
"figure",
"table",
"figure\\*",
"table\\*"
]
数式を置き換えるための設定
数式を固定の文字列に置き換えます。
以下の設定だと$eq 1$
や、\begin{align} eq 2\end{align}
等をそれぞれ[FORMULA]
に置き換えます。
"texToTxtConverter.formulaPlaceholder": "[FORMULA]",
"texToTxtConverter.formulaPatterns": [
"\\\\\\((.*?)\\\\\\)",
"\\\\\\[.*?\\\\\\]",
"\\$\\$(.*?)\\$\\$",
"\\$(.*?)\\$",
"\\\\begin{equation}(.*?)\\\\end{equation}",
"\\\\begin{equation\\*}(.*?)\\\\end{equation\\*}",
"\\\\begin{align}(.*?)\\\\end{align}",
"\\\\begin{align\\*}(.*?)\\\\end{align\\*}",
"\\\\begin{eqnarray}(.*?)\\\\end{eqnarray}",
"\\\\begin{eqnarray\\*}(.*?)\\\\end{eqnarray\\*}",
]
まとめ
VS Code上でTeXの変換が済むので便利だと思っています。
突貫工事なのでまだまだイマイチなところには優しく目をつぶっていただけると嬉しいです。