TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

    \setcounter{\numberofparagraphs}{ ???? }  
        { \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
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.


            \let\origpar\par% Might not be required

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



test test test

test test test
test test test

test test test

test test test

test test test
test test test

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


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.