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?

TeXの数式やコマンドを削除・置き換えて平易な文に変換するVSCode拡張機能

Last updated at Posted at 2025-01-26

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.

こんな感じ:
output_video.gif

設定方法

消したいブロックの設定

コメントなど削除したいブロックの開始と終了部分を設定します。
以下のように設定すると、\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の変換が済むので便利だと思っています。
突貫工事なのでまだまだイマイチなところには優しく目をつぶっていただけると嬉しいです。

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?