Count the number of lines of a long title with a specific line width

For counting number of lines internal TeX counter \prevgraf is available that contains number of lines for currently formed or just closed paragraph (see The TeXbook, chapter 14). So this code:

\documentclass{article}
\newcounter{numbertitlelines}
\newlength{\titlelength}
\newbox\titlebox % auxiliary box
\newcount\titlelines % auxiliary counter
\newcommand{\titleformat}{\bfseries\centering} % format of title output
\newcommand*{\titlelinescount}[2]{%
    \setbox\titlebox=\vbox{\hsize=#1 \titleformat
        #2\par
        \global\titlelines=\the\prevgraf
    }
    \setcounter{numbertitlelines}{\the\titlelines}
}
\begin{document}

% -- Test --
\newcommand{\Title}{ A VARY COMMON \LaTeX\ THESIS TEMPLATE OF COMMUNICATION UNIVERSITY OF ZHEJIANG}
\newlength{\parlength}
\setlength{\parlength}{0.35\textwidth}

\titlelinescount{\parlength}{\Title}

The title \parbox{\parlength}{\titleformat\Title} contains \thenumbertitlelines\ lines


\end{document}

makes this result:

enter image description here

Macro \titleformat contains settings for title output providing a correspondence with title format in counting command. I set \bfseries\centering for example.

Auxiliary TeX counter \titlelines serves to globally save \prevgraf value in \vbox. Of course, you can use this counter directly if you want, without numbertitlelines counter.

Tags:

Count

Line