I would like to have a macro that counts the number of lines in a block of text. I was able to create one based on the answer for "counting the newlines in title", but I would prefer that the macro returns the number of lines directly rather than having to retrieve it via \numlines. If anyone knows how to do this I would appreciate being told how.
\documentclass{article}
\newcommand\countlines[1]{
{\setbox0\vbox{\noindent{#1}\par
\count@\z@
\loop
\unskip\unpenalty\unskip\unpenalty\unskip
\setbox0\lastbox
\ifvoid0
\xdef\numlines{\the\count@}
\else
\advance\count@\@ne
\repeat}}
}
\title{aaa\\b\\bb}
\begin{document}
\makeatletter
\countlines{\@title}
\typeout{Number of lines is \numlines}
\end{document}
What I specifically desire is that \countlines return the number of lines directly as indicated below.
\typeout{Number of lines is \countlines{\@title}}

\numlinesinto the end of the macro definition. – cfr Jul 03 '16 at 02:43\newlinethe count becomes wrong? – Cfun Jul 03 '16 at 04:55