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

Code, edit and compile here:
\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}%
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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