5

Previously, I asked a question about highlighting pages after the n'th page. This was because I had to adhere to a page limit and wanted to indicate when a page went over that limit.

I now have a similar problem, except I have a word limit. I would like to highlight (e.g. with a red background) all words in an environment after n words have appeared in it.

In the below MWE, the words ABORT should be highlighted and all other text should be normal.

\documentclass{article}

\newenvironment{limitwords}{}{}
% ...code to set a limit of 20 in the above env...


\begin{document}

\begin{limitwords}
Hello, this is a sample text.
Oh no, we are going to run out of budget!

Help! Help! Here we ABORT ABORT ABORT ABORT ABORT

ABORT
\end{limitwords}

Luckily, outside of the environment, we can write more than
20 words without anything bad happening. And what's more, if we
start another such environment, it will allow 20 words again.

\begin{limitwords}
Hello, world!
\end{limitwords}

\end{document}

1 Answer 1

4

Use colors and words to your choice. If it is for some weird University requirement, you can take them to the cleaners by using either very short words in valid sentences, like I did or very long ones! If it is for some computing class, use Javanese, Khmer or Chinese as they dont have word dividers:)

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\ExplSyntaxOn
\cs_set:Npn\l_process_aux#1{
\seq_set_split:Nnn \l_tmpa_seq {~} {#1}
\seq_map_inline:Nn\l_tmpa_seq 
{ \int_incr:N\l_tmpa_int 
  \int_compare:nNnTF{\l_tmpa_int}>{20}{{\color{blue}##1}~}
  {##1~}
 }
}
\NewDocumentEnvironment{limitwords}{+b}
{\l_process_aux{#1}}{}
\ExplSyntaxOff
\begin{limitwords}
So, if it is in or if it is on, it is as it is,be it in or be it on.\\
ABORT ABORT
\end{limitwords}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .