> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/latex/zh-cn/zhi-shi-ku/099-is-there-a-way-to-run-a-word-count-that-doesn-t-include-latex-commands.md).

# 有没有办法进行不包含 LaTeX 命令的字数统计？

## 简介

是的，你可以通过选择 `菜单` 然后 `字数统计` ，如下这个简短的屏幕录像所示：

![Wordcount.gif](/files/b13525bb7f933c1d877eefbe61a64248fb300e48)

这将运行 [texcount 工具](http://app.uio.no/ifi/texcount) 来统计项目主文件中的字数，以及通过 `\include` 和 `\input`导入到项目主文件中的任何文件的字数。请注意，texcount 要在 Overleaf 上正常工作，你的项目主文档必须位于项目顶层（即不在文件夹内）。

## 在字数统计中包含参考文献

默认情况下，参考文献、页眉、标题说明、浮动体、显示公式等都不会计入统计。要包含引文和参考文献：

1. 添加 `%TC:incbib` 到你的 .tex 文件导言区，然后
2. 在你的项目中添加一个名为 latexmkrc（无文件扩展名）的文件，并在其中加入下面这一行（将“main”改成你的主 .tex 文档文件名）

   ```perl
   END { system('cp', 'output.bbl', 'main.bbl'); }
   ```

**请注意，这个变通方法只适用于你使用的是手动 `thebibliography` 列表或 BibTeX。** [**texcount 目前不支持 biblatex**](https://tex.stackexchange.com/q/102882/226)**.**

参见 [#使用自定义参数运行 texcount](#run-texcount-with-custom-parameters) 用于显示页眉、标题说明、浮动体、显示公式等的字数统计。

## 忽略某些部分

有时，你可能希望把某些章节或区域排除在字数统计之外。你可以在这些区域周围放置 `%TC:ignore` 和 `%TC:endignore` 。例如：

```latex
%TC:ignore
\maketitle

\begin{abstract}
...因此标题页和摘要不会被计入字数...
\end{abstract}
%TC:endignore
```

## 使用自定义参数运行 texcount

如果你想使用自己的一套 [texcount 参数](http://app.uio.no/ifi/texcount/documentation.html) 来自定义输出，或进行字符统计，或包含标题说明等，你可以通过在项目中定义几个命令来实现（不过这会涉及使用 LaTeX 命令...）。例如：

```latex
\usepackage{verbatim}

\newcommand{\detailtexcount}[1]{%
  \immediate\write18{texcount -merge -sum -q #1.tex output.bbl > #1.wcdetail }%
  \verbatiminput{#1.wcdetail}%
}

\newcommand{\quickwordcount}[1]{%
  \immediate\write18{texcount -1 -sum -merge -q #1.tex output.bbl > #1-words.sum }%
  \input{#1-words.sum} words%
}

\newcommand{\quickcharcount}[1]{%
  \immediate\write18{texcount -1 -sum -merge -char -q #1.tex output.bbl > #1-chars.sum }%
  \input{#1-chars.sum} characters (not including spaces)%
}
```

假设你的文件名为 `main.tex`，那么在你的项目中调用这些命令后，PDF 中会出现如下输出。输出包含每个子（章节）的细分，以及标题说明、浮动体、显示公式等。请注意，由于 Overleaf 构建流程的配置方式，你必须在 \`texcount\` 命令中包含 \`output.bbl\`，这样 BibTeX 生成的参考文献列表也才能被计入。

```latex
% 不要统计这些！
%TC:ignore
\quickwordcount{main}
\quickcharcount{main}
\detailtexcount{main}
%TC:endignore
```

![Custom-texcount.png](/files/1b4cc00f5912a7e0e4cbaedd48ffa69bb1a3bbe1)

请参见 [texcount 文档](http://app.uio.no/ifi/texcount/documentation.html#options) 了解每个运行时标志的作用。你可以看到这些示例的实际效果 [这里](https://www.overleaf.com/read/dqvrqghbtmbc).

“页眉中的词数”和“页眉”在按章节细分的统计中用于 `\section`, `\subsection` 等等，但不包括 1、1.1 等编号。

“正文之外的词数”和“标题说明”在按章节细分的统计中用于 `\caption`，但不包括“Table 1”等前缀。

默认情况下，行内引文不会被计数。如果你想把它们计入统计，请把下面这些行添加到你的导言区：

```latex
%TC:macro \cite [option:text,text]
%TC:macro \citep [option:text,text]
%TC:macro \citet [option:text,text]
% ... and for any other \cite commands you may use
```

不过，请注意每个 `\citep{ddd}` 在 texcount 中只算一个词（这多少说得通；否则你可以通过引用多作者文献人为地大幅提高字数统计）。因此 `\cite{faye1996}`虽然显示为 (Fay, 1996)，但在 texcount 中算作一个词，而在其他文字处理器中算作 2 个词。

如果要把表格和 tabular 环境中的词也计入“正文中的词数”统计，你可以 [再添加这些 texcount 指令](https://tex.stackexchange.com/a/37777/226) ：

```latex
%TC:group table 0 1
%TC:group tabular 1 1
```

## 在 texcount 中使用 \import 命令

默认情况下，texcount 能识别诸如 `\input`, `\include` 这类用于统计已包含子文件的常见命令。不过，命令 `\import` 来自 `import` 宏包默认不受支持。

因此，如果你在文档中使用 `\import` ，不妨改用更常见的 `\input` 和 `\include` 命令，texcount 就能正常工作，无需进一步修改。

如果你确实更希望继续使用 `\import`，你可以在导言区添加一条 texcount 指令，使得 `\import{foo}{bar}` 在 texcount 看来就像 `\include{foo/bar}` ，这样它就会统计子文件中的词数：

```latex
%TC:fileinclude \import dir,file
```

请注意，在添加这一行之后，你需要先重新编译一次，然后再点击“字数统计”。

## 进一步阅读

* [完整的 texcount 文档](http://app.uio.no/ifi/texcount/documentation.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/latex/zh-cn/zhi-shi-ku/099-is-there-a-way-to-run-a-word-count-that-doesn-t-include-latex-commands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
