計算一個部分中的字元數並檢查範圍

計算一個部分中的字元數並檢查範圍

我想計算一個部分中的字元數,然後顯示該值是否低於、等於或高於限制。我已經從以下位置獲取了代碼動態計算並傳回一個部分中的單字數。我有以下內容(用 編譯pdflatex --shell-escape mwe.tex)。

\documentclass{article}

\newcommand{\wordcount}{%
    \immediate\write18{texcount -merge -sub=section \jobname.tex  | grep "Section" | sed -e 's/+.*//' | sed -n \thesection p > 'count.txt'} \input{count.txt}}

\usepackage{lipsum}

\begin{document}

\section{test1}
\wordcount
\ifnum\wordcount<1000 Below 
\ifnum\worcount>2000 Above
\else Inside

foo bar


\section{test2}
\wordcount

foo bar

foo bar

foo bar

\end{document}

上面的程式碼不起作用,因為\wordcount它並不是 LaTeX 意義上的真正值。有沒有一種方法可以將我轉換為實際值以使程式碼正常工作?

其他解決方案是使用內部計算字數的環境,但在這種情況下,我需要保存每個計數器的值並稍後顯示它。

答案1

Perl 腳本 TeXcount (http://app.uio.no/ifi/texcount/) 將產生 TeX 文件的字數統計,並按部分進行細分。一個例子如下所示。您也可以省略 .tex 檔案並新增 -help 標誌來查看無數選項。

$ perl texcount.pl paper.tex 

File: paper.tex
Encoding: ascii
Words in text: 5824
Words in headers: 22
Words outside text (captions, etc.): 1032
Number of headers: 8
Number of floats/tables/figures: 18
Number of math inlines: 48
Number of math displayed: 0
Subcounts:
  text+headers+captions (#headers/#floats/#inlines/#displayed)
  90+12+0 (1/0/6/0) _top_
  162+1+0 (1/0/1/0) Section: Abstract
  751+1+0 (1/0/1/0) Section: Introduction
  795+1+0 (1/0/6/0) Section: Methods
  3964+2+0 (1/0/20/0) Section: Results \& Discussion
  44+1+0 (1/0/0/0) Section: Acknowledgments
  0+2+320 (1/9/0/0) Section: Table Legends
  18+2+712 (1/9/14/0) Section: Figure Legends

相關內容