14

I see many cls files using plus and minus operators to set lengths. How can I use these operators (or some equivalent macro(s)) to do something like the following?

\setlength{\lengtha}{.5\textwidth}
\setlength{\lengthb}{.5\textwidth}
\begin{tabular*}{\lengtha plus \lengthb}{cc}
a & b \\
\end{tabular*}
  • 6
    The plus and minus operators that you see in cls and other files describe examples of stretchable glue. You can find many descriptions of it, this post has a good example and explanation to get you started :) – cmhughes Oct 25 '11 at 19:24
12

Use the calc package. Package description:

Adds infix expressions to perform arithmetic on the arguments of the LaTeX commands \setcounter, \addtocounter, \setlength, and \addtolength.

\documentclass{article}

\usepackage{calc}

\newlength{\lengtha}
\setlength{\lengtha}{.5\textwidth}
\newlength{\lengthb}
\setlength{\lengthb}{.5\textwidth}

\begin{document}

\begin{tabular*}{\lengtha + \lengthb}{cc}
\hline
a & b \\
\hline
\end{tabular*}

\end{document}
  • Thanks -- this is also convenient. Seems like it cannot be used in all places though (for instance, setting a new length in a list environment...), but will keep in mind. – hatmatrix Oct 25 '11 at 20:13
14

To perform simple arithmetic operations with lengths you can use the e-TeX primitive \dimexpr:

\documentclass{article}

\newlength{\lengtha}
\setlength{\lengtha}{.5\textwidth}
\newlength{\lengthb}
\setlength{\lengthb}{.5\textwidth}

\begin{document}

\noindent\begin{tabular*}{\dimexpr\lengtha+\lengthb\relax}{cc}
\hline
a & b \\
\hline
\end{tabular*}

\end{document}

For further details, please refer to the e-TeX manual.

Something different is the use of plus and minus to specify glue in TeX:

<dimen>plus<dimen>minus<dimen>

where the plus<dimen> and minus<dimen> are optional and set to zero if not present; plus introduces stretchability, minus introduces shrinkability.

  • Yes! This worked well and thanks for the explanation. – hatmatrix Oct 25 '11 at 20:12

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.