5

Here's my pseudo latex code to try and show what I'm trying to do in my memo latex class:

\newcommand\headername{%should be the @memoto, or if too long, an abbreviated subject
    \ifthenelse{\lengthtest{\settowidth{\@memoto}>.45*\linewidth}}
        {\emph{dummy text here}}
        {\@memoto}
}

I'm probably missing some calculation package or other, but I'm looking for some kind of test to see if \@memoto is longer than 45% of the linewidth. How do I do this?

(ultimately I will use this latex class within LyX, but I hope this is general enough to be answered without regard to LyX)

  • 3
    I don't think you need * in .45\linewidth – Guido Feb 1 '13 at 4:48
7

Here is an option:

enter image description here

\documentclass{article}
\makeatletter
\newcommand{\memoto}{\@dblarg\memoto@aux}
\def\memoto@aux[#1]#2{%
  \def\@memoto@short{#1}% Short \@memoto
  \def\@memoto@long{#2}% Long \@memoto
}
\newcommand\headername{% should be \@memoto, or if too long, an abbreviated subject
  \setbox9=\hbox{\@memoto@long}%
  \ifdim\wd9>.45\linewidth
    \emph{\@memoto@short}%
  \else
    \@memoto@long%
  \fi
}
\makeatother
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\rule{.45\textwidth}{1pt}\par
\memoto[Here is some short text]{Here is some longer text}\headername\par
\memoto[Here is some short text]{Here is some much longer text}\headername\par
\memoto[Here is some short text]{Here is some very much longer text}\headername\par
\memoto[Here is some short text]{Here is some extremely much longer text}\headername\par
\rule[1ex]{.45\textwidth}{1pt}\par
\end{document}

The definition of \memoto uses \@dblarg to allow for easily specifying an optional argument (see What does the command \@dblarg in a class?) and uses this shorter text if the length of the longer text is greater than .45\textwidth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.