如何创建一个显示其包含的行数的段落?

如何创建一个显示其包含的行数的段落?

尽量简短...

如何创建显示元信息的段落,例如,提及其包含的行数或字数,使用TeX 基元仅此而已?非常重要的一点是,此类信息可以出现在段落的任何地方(在段落的开头、中间或结尾......任何地方)

例如:

本段共4行。 Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá- Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá -Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá -Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá-Blá

(我不确定上面的段落是否连贯。这就是这个问题的重点!)

我曾想过创建一个 \vbox,做一些计算,然后在更新后将其解包。但元数据可能会占用很多行。我不知道如何预览 TeX 系统将在哪里换行或使用连字符,因此,在给定点插入一些字符后将添加多少行。

提前致谢。

答案1

您可以在上下文中测量线条\setbox0=\vbox

\newcount\linenum
\def\linecount #1\par{%
   \linenum=0
   \setbox0=\vbox{#1\par\global\linenum=\prevgraf}%
   \setbox0=\vbox{#1\par\global\linenum=\prevgraf}%
   #1\par
}
\def\lines{\the\linenum}

\linecount The paragrpaph has \lines\ lines. 
Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá
Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá
Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá
Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá
Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá Blá
Blá Blá Blá Blá Blá

\end

\linenum我进行了两次测量,因为当知道寄存器的数字时,行数就可以改变。

注意,这\newcount不是 TeX 基元。如果您只需要 TeX 基元,则使用\countdef。所有其他构造都只是 TeX 基元。

答案2

您可以使用\label\ref,或等效概念。需要两次传递,因为直到段落处理完毕\prevgraf后才可用。\par

\documentclass{article}

\newcounter{countpar}

\makeatletter
\newcommand{\lines}{%
  % redefine \par to do the count
  \stepcounter{countpar}%
  \def\par{%
    \@@par % finish the paragraph
    \edef\@currentlabel{\the\prevgraf}%
    \label{lines@\thecountpar}%
    \let\par\@@par
  }%
  This paragraph has~\textbf{\ref{lines@\thecountpar}} lines%
}
\makeatother

\begin{document}

\lines.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.

\lines.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.

Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
\lines.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.

Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
Some text follows. Some text follows. Some text follows.
\lines.

\end{document}

在此处输入图片描述

相关内容