GeneralExcluding Some text from Counted Environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Excluding Some text from Counted Environment

Post by rspringuel »

In the MWE below I've defined an environment that counts the number of words inside based on this question over on StackExchange. I'd like to modify it further to allow me to exclude blocks of text from the count (by defining \exclude appropriately). Does anyone have any suggestions on how to best go about this?

Code: Select all

% !TEX program = XeLaTeX
\documentclass{article}

\usepackage{xesearch}

\newcounter{counted}% for creating unique counted* macros
\newcounter{words}[counted]% let words counter be reset by counted
\makeatletter
\newenvironment{counted}{%
    \newcommand{\exclude}[1]{##1}% this command should prevent the words in its argument from being included in the word count.
    \par\noindent\hrulefill\par
    \stepcounter{counted}%
    \@ifundefined{counted\thecounted}{%
        \marginpar{\footnotesize\itshape?? words}%
    }{%
        \marginpar{\footnotesize\itshape\@nameuse{counted\thecounted} words}%
    }%
    \SearchList!{wordcount}{\stepcounter{words}}
        {a?,b?,c?,d?,e?,f?,g?,h?,i?,j?,k?,l?,m?,
        n?,o?,p?,q?,r?,s?,t?,u?,v?,w?,x?,y?,z?}
    \UndoBoundary{'}
    \SearchOrder{p;}}{%
    \StopSearching
    \immediate\write\@auxout{\string\global\string\@namedef{counted\thecounted}{\thewords}}%
    \marginpar{\raggedright\footnotesize\textit{End counting words}}
    \par\noindent\hrulefill}
\makeatother



\begin{document}
\begin{counted}
        In order to better understand this sentence we will expand the predicate by replacing each term with its definition.  “Reverence” is an intentional action directed towards an object (using “object” in the broadest sense of the word so that it includes everything which can be thought of) for the purpose of honoring that object and/or acknowledging the subordination of the subject to the object (the actual mix being determined on a case-by-case basis).  The modification by “natural” in the sentence indicates that this action is not one which is learned, but rather arises from the essence of the subject performing the action.  The possessive “mind's” further modifies this action to indicate that it is one involving the active mental faculties.  Further it restricts the possible domain of subjects to those which possess said active mental faculties.  Putting all this together, the predicate can be expanded as “the intentional actions of a being with active mental faculties which direct said faculties towards an object for the purpose of honoring or subordinating the subject to said object and which arise from the essence of the being possessing said faculties.”
\exclude{These seven words should not be counted.}
\end{counted}
When working correctly, the count above should be 187, not 193.
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Excluding Some text from Counted Environment

Post by rais »

The counterpart to \StopSearching is \StartSearching. What's keeping you from trying

Code: Select all

        \newcommand{\exclude}[1]{\StopSearching ##1\StartSearching}% this command should prevent the words in its argument from being included in the word count.
KR
Rainer
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Re: Excluding Some text from Counted Environment

Post by rspringuel »

That worked perfectly.

This environment (and the xesearch package it's based on) is something of a black box for me. I use it, but I don't know exactly what it's doing or why it works (during the actual counting process). I suppose I should take a better look at it, but don't really have the time right now.

Thanks for the help.
Post Reply