如何计算 latex 中特定环境中的字符数

如何计算 latex 中特定环境中的字符数

在特定环境下是否可以检查字符数是否超过 1800 个字符?如果大于该值,则应抛出错误?如何实现?

我的 MWE 是

\documentclass{book}
\usepackage{color}
\begin{document}
\definecolor{gray}{cmyk}{0,0,0,0.3}
\newbox\absbox
\newbox\tempbox
\newdimen\tempdima

\newenvironment{abstract}{\global\setbox\absbox\vbox\bgroup}{\egroup}

\def\printabstract{\par%
                    \setbox\tempbox\hbox{Keywords}%Zusammenfassung
                        \tempdima\wd\tempbox%
                        \advance\tempdima4.2mm%
                        \advance\tempdima4.2mm%
                        \noindent\fboxsep0pt\colorbox{gray}{\vbox to 13pt{\hsize\tempdima\vfill\noindent\hspace*{4.2mm}Abstract\vfill}}
                         \endgraf\vspace*{2.8pt}%
                         {\noindent\ignorespaces\unvcopy\absbox\endgraf}
                 }




\begin{abstract}
Using the standard definitions of EVM and ES, we established a sound theoretical basis for the prediction of the project duration when the cost profiles follow a Gompertz function. We  derived formulas for the duration estimates and found the important and interesting result that a simple 2-point estimation formula is effective in predicting the duration early in the project.

In conclusion, we established a new, effective method of duration and cost estimation over time and validated the theory by comparing its predictions to many synthetic projects. The duration and cost error formulas are quite simple and require little additional effort to project teams to be practically useful in the context of project monitoring and control.

In conclusion, we established a new, effective method of duration and cost estimation over time and validated the theory by comparing its predictions to many synthetic projects. The duration and cost error formulas are quite simple and require little additional effort to project teams to be practically useful in the context of project monitoring and control.
\end{abstract}

\chapter{Sample Chapter}

Text, Text, Text, Text,


\section{Section 1}

\printabstract


\section{Section 2}

\printabstract \end{document} 

答案1

它实际上计算的是非空格标记。我countenv使用以下语法来介绍环境

\begin{countenv}[<error-msg prepend>]{environment name}

因此,如果超出限制,则\begin{countenv}{abstract}足以给出正确的错误消息。但是,需要可选参数,因为任何错误消息都必须在错误消息前面加上。abstract\begin{countenv}[\item]{itemize}\item

环境大小限制在计数器中设置charlim

\documentclass{book}
\usepackage{color}
\usepackage{environ}
\newcounter{charcount}
\newcounter{charlim}
\setcounter{charlim}{1800}
\NewEnviron{countenv}[2][]{%
  \setcounter{charcount}{0}%
  \expandafter\countem\BODY\relax\EOE
  \relax%
  \ifnum\value{charcount}<\value{charlim}\relax
    \begin{#2}\BODY\end{#2}
  \else
    \begin{#2}#1 \thecharcount{} letters in the ``#2'' exceeds
       \thecharlim{} character limit\end{#2}
  \fi
}

\long\def\countem#1#2\EOE{%
  \stepcounter{charcount}%
  \ifx\relax#2
    \def\next{\relax}%
  \else
    \def\next{\countem#2\EOE}%
  \fi
  \expandafter\next%
}

\begin{document}
\definecolor{gray}{cmyk}{0,0,0,0.3}
\newbox\absbox
\newbox\tempbox
\newdimen\tempdima

\newenvironment{abstract}{\global\setbox\absbox\vbox\bgroup}{\egroup}

\def\printabstract{\par%
                    \setbox\tempbox\hbox{Keywords}%Zusammenfassung
                        \tempdima\wd\tempbox%
                        \advance\tempdima4.2mm%
                        \advance\tempdima4.2mm%
                        \noindent\fboxsep0pt\colorbox{gray}{\vbox to 13pt{\hsize\tempdima\vfill\noindent\hspace*{4.2mm}Abstract\vfill}}
                         \endgraf\vspace*{2.8pt}%
                         {\noindent\ignorespaces\unvcopy\absbox\endgraf}
                 }

\begin{countenv}{abstract}
Using the standard definitions of EVM and ES, we established a sound theoretical basis for the prediction of the project duration when the cost profiles follow a Gompertz function. We  derived formulas for the duration estimates and found the important and interesting result that a simple 2-point estimation formula is effective in predicting the duration early in the project.

In conclusion, we established a new, effective method of duration and cost estimation over time and validated the theory by comparing its predictions to many synthetic projects. The duration and cost error formulas are quite simple and require little additional effort to project teams to be practically useful in the context of project monitoring and control.

In conclusion, we established a new, effective method of duration and cost estimation over time and validated the theory by comparing its predictions to many synthetic projects. The duration and cost error formulas are quite simple and require little additional effort to project teams to be practically useful in the context of project monitoring and control.
\end{countenv}

\chapter{Sample Chapter}

Text, Text, Text, Text,

\section{Section 1}

\printabstract

\section{Section 2}

\begin{countenv}[\item]{itemize}
\item this is a test

\item another test

\item a final test
\end{countenv} 

\end{document} 

在 20 个字符的限制内,\setcounter{charlim}{20}

在此处输入图片描述

请注意,环境中报告的 37 个字母itemize实际上是 32 个字母数字字母,加上 2 个\par标记和 3 个\item标记(在这种情况下会产生项目符号,但在其他情况下enumerate,可能会分别产生多字符输出)。


在 800 个字符的限制下,\setcounter{charlim}{800}

在此处输入图片描述


字符数限制为 1800 个,\setcounter{charlim}{1800}

在此处输入图片描述

相关内容