Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.


Is there a way to count the number of paragraphs in a command argument? I would like to make a command doing something like this:

\newcounter{\numberofparagraphs}  
\newcommand{\testcmd}[1]{  
    \setcounter{\numberofparagraphs}{ ???? }  
    \ifthenelse{\equal{\numberofparagraphs}{1}}
        { \testcmdOneParagraph{#1} }
        { \testcmdMoreThenOneParagraph{#1} }  
}

So if the input argument is only one paragraphs the command will do one thing and otherwise it will do another thing.

Is this possible?

share|improve this question
    
Inside a macro all lines ending with } and { should have a % after them to avoid unwanted spaces. Line breaks count as spaces. There should be also no spaces inside the { } as long you do want them there explicitly. –  Martin Scharrer Apr 21 '11 at 13:28
    
Paragraphs can be started by explicit and implicit \par commands. To figure out how many are in one argument you would need to typeset the content with \par redefined to increase a counter. This would be good enough for detecting one versus more paragraphs, but wouldn't yield the exact number in all cases because \par\par would only create one new paragraph, not two. –  Martin Scharrer Apr 21 '11 at 13:34
    
So I have to redefine \par in the beginning and the end of the command, but how do I pass the information on from the first typeset to the next? –  Richard Lindquist Capion Apr 21 '11 at 14:03

1 Answer 1

up vote 2 down vote accepted

The following code seems to work in my simple tests. The argument is typeset inside a saved box which is never used while the \par is increasing a counter. LaTeX's \addtocounter increased the counter globally, so the change will survive after the box and group ends. Note that this typesets the content twice, which is not that efficient and might have hidden side effects. However, the risk is low because the code is executed in a group and is never really added to the document, so that \labels etc. are never written to the .aux file.

I'm using a minipage so that the material is typeset in a mostly normal environment which can hold multiple paragraphs. I'm not 100% sure if this is required or correct, but it does the job as far I can see.

\documentclass{article}

\newcounter{numberofparagraphs}
\makeatletter
\newcommand{\testcmd}[1]{%  
    \setcounter{numberofparagraphs}{0}%
    {\sbox\@tempboxa{%
        \begin{minipage}{\linewidth}%
            \let\origpar\par% Might not be required
            \def\par{\origpar\addtocounter{numberofparagraphs}{1}}%
            #1%
        \end{minipage}%
    }}%
    \ifnum\value{numberofparagraphs}<2
        \testcmdOneParagraph{#1}%
    \else
        \testcmdMoreThenOneParagraph{#1}%
    \fi
}
\makeatother

\newcommand{\testcmdOneParagraph}[1]{Only One: #1}
\newcommand{\testcmdMoreThenOneParagraph}[1]{More: #1}

\begin{document}

\testcmd{test}

\testcmd{%
test test test
}

\testcmd{%
test test test
%
test test test
}

\testcmd{%
test test test

test test test
}


\testcmd{%
test test test
\par
test test test
}


\end{document}
share|improve this answer
    
It's working great! Thanks a lot for your help Martin! –  Richard Lindquist Capion Apr 21 '11 at 16:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.