> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-pro.ayaka.space/latex/zh-tw/zhi-shi-ku/099-is-there-a-way-to-run-a-word-count-that-doesn-t-include-latex-commands.md).

# 有方法執行不包含 LaTeX 指令的字數統計嗎？

## 簡介

是的，你可以透過選擇 `選單` ，然後 `字數統計` ，如下這段簡短的螢幕錄影所示：

![Wordcount.gif](/files/5dbe9b04a9961a82de989ba3c639f45e68841bbc)

這會執行 [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 的建置流程設定方式，若要同時計入由 BibTeX 產生的參考文獻清單，你必須在 \`texcount\` 命令中包含 \`output.bbl\`。

```latex
% 這些不要計算！
%TC:ignore
\quickwordcount{main}
\quickcharcount{main}
\detailtexcount{main}
%TC:endignore
```

![Custom-texcount.png](/files/52517eda0c06f44e84759af41478f0eecd1d575c)

請參閱 [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]
% ... 以及你可能使用的任何其他 \cite 命令
```

不過請注意，每個 `\citep{ddd}` 在 texcount 中都只算作一個字（這其實某種程度上合理；否則你可以透過引用含有多位作者的參考文獻，人工大幅增加字數統計）。因此 `\cite{faye1996}`雖然顯示為（Fay，1996），但在 texcount 中只算作一個字，而在其他文字處理器中則算作 2 個字。

若要將表格與表格環境中的字詞計入「正文中的字詞」統計，你可以 [也加入這些 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://overleaf-pro.ayaka.space/latex/zh-tw/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.
