Document ClassesBetter way to measure text width and height?

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
Berticus
Posts: 8
Joined: Wed Dec 31, 2008 11:37 pm

Better way to measure text width and height?

Post by Berticus »

I've got some really ugly looking code and was hoping someone with a LOT more experience in LaTeX could help me clean it up. What it does is it calculates how wide the minipage width should be to contain the text.

Code: Select all

\renewcommand{\maketitle}{%
%    Establish lengths
              \newlength{\@contactinfowidth}%
              \settowidth{\@contactinfowidth}{\@apt}%
              \newlength{\@contactinfoheight}%
              \settoheight{\@contactinfoheight}{\@apt}%
              \newlength{\@addresswidth}%
              \settowidth{\@addresswidth}{\@address}%
              \newlength{\@addressheight}%
              \settoheight{\@addressheight}{\@height}%
              \newlength{\@citystatezipwidth}%
              \settowidth{\@citystatezipwidth}{\@citystatezip}%
              \newlength{\@citystatezipheight}%
              \settoheight{\@citystatezipheight}{\@citystatezip}%
              \newlength{\@webpagewidth}%
              \settowidth{\@webpagewidth}{\@webpage}%
              \newlength{\@webpageheight}%
              \settoheight{\@webpageheight}{\@webpage}%
              \newlength{\@emailwidth}%
              \settowidth{\@emailwidth}{\@email}%
              \newlength{\@emailheight}%
              \settoheight{\@emailheight}{\@email}%
              \newlength{\@telephonewidth}%
              \settowidth{\@telephonewidth}{\@telephone}%
              \newlength{\@telephoneheight}%
              \settoheight{\@telephoneheight}{\@telephone}%
              \newlength{\@authorheight}%
              \settoheight{\@authorheight}{\@author}%
%    Determine the longest length of the contact information
              \ifthenelse{\@addresswidth > \@contactinfowidth}{\setlength{\@contactinfowidth}{\@addresswidth}}{}%
              \ifthenelse{\@citystatezipwidth > \@contactinfowidth}{\setlength{\@contactinfowidth}{\@citystatezipwidth}}{}%
              \ifthenelse{\@webpagewidth > \@contactinfowidth}{\setlength{\@contactinfowidth}{\@webpagewidth}}{}%
              \ifthenelse{\@emailwidth > \@contactinfowidth}{\setlength{\@contactinfowidth}{\@emailwidth}}{}%
              \ifthenelse{\@telephonewidth > \@contactinfowidth}{\setlength{\@contactinfowidth}{\@telephonewidth}}{}%
%    Determine remaining length and use it as the space for the author's name
              \newlength{\@authorwidth}%
              \setlength{\@authorwidth}{\textwidth}%
              \addtolength{\@authorwidth}{-\@contactinfowidth}%
%    Determine height of contactinfo section
              \addtolength{\@contactinfoheight}{\@addressheight}%
              \addtolength{\@contactinfoheight}{\@citystatezipheight}%
              \addtolength{\@contactinfoheight}{\@webpageheight}%
              \addtolength{\@contactinfoheight}{\@emailheight}%
              \addtolength{\@contactinfoheight}{\@telephoneheight}%
%    Align horizontal rule to bottom of contact info
              \newlength{\@hrulespace}%
              \setlength{\@hrulespace}{\@contactinfoheight}%
              \addtolength{\@hrulespace}{-\@authorheight}%
              \newlength{\@hrule}%
              \setlength{\@hrule}{\@authorwidth}%
              \addtolength{\@hrule}{-10pt}%
%    Create the header
              \begin{minipage}{\@authorwidth}
                  \textbf{\Huge{\MakeUppercase{\@author}}}\\[\@hrulespace]
                  \ifthenelse{\@contactinfoheight > \@authorheight}{\rule[5pt]{\@hrule}{0.5pt}}{}
              \end{minipage}%
              \begin{minipage}{\@contactinfowidth}
                  \ifthenelse{\equal{\@apt}{}}{}{\@apt \\} \ifthenelse{\equal{\@address}{}}{}{\@address \\} \ifthenelse{\equal{\@citystatezip}{}}{}{\@citystatezip \\} \ifthenelse{\equal{\@webpage}{}}{}{\@webpage \\} \ifthenelse{\equal{\@email}{}}{}{\@email \\} \ifthenelse{\equal{\@telephone}{}}{}{\@telephone}
              \end{minipage}%
              \vspace{14pt}\\%
}
Is there an easier way to do this? This isn't the only command/environment that changes based on the text it's holding.

Recommended reading 2024:

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

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

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: Better way to measure text width and height?

Post by phi »

If the code works for you, then I see no point in trying to optimize it. It's fairly clean and not very long. Sure, you could cut a few lines, but only if you are willing to go deep into TeX internals. But this would only make the code less readable.
Post Reply