LoginSignup
10
5

More than 3 years have passed since last update.

LaTeX で動的に単語数をカウント

Last updated at Posted at 2019-11-09

texcount コマンドで単語数をカウントできる。

$ texcount main.tex

単に総単語数のみを出力させるには、下記のオプションをつける。

$ texcount main.tex -inc -sum -1

さらに、この機能をbashful パッケージを用いて文書内で動的に行う。

\documentclass{article}
\usepackage{bashful}

\bash
texcount main.tex -inc -sum -1
\END
\let\WordCount\bashStdout

\begin{document}

\section{Dynamic word counter}

This document has {\WordCount} words.
\end{document}
  • \bash ... \END の中にシェルスクリプトを書くと、コンパイル時に実行される。結果が \bashStdout に格納されるので、これを \WordCount という別の変数に定義し直している。\bashStdout を直接用いても良いが、その場合別のシェルスクリプトを実行すると値が上書きされてしまう。\letによる評価はその場で行われるため、\WordCountに格納された値は \bashStdoutの値が変更されても保存される。
  • \bash ... \END\begin{document} より前に書くと良い。後ろに書くと、そこも文書内とみなされて、シェルコマンドの単語数もカウントされる。

ただし、シェルスクリプトの実行は危険を伴うので、コンパイル時に -shell-escape オプションを追加して明示に許可する必要がある。

$ latex -shell-escape main.tex

出力結果:

例(Overleaf): word count test

10
5
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
10
5