Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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*}
share|improve this question
5  
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
    
Ok, this makes more sense. Thank you. –  crippledlambda Oct 25 '11 at 20:08

2 Answers 2

up vote 9 down vote accepted

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}
share|improve this answer
    
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. –  crippledlambda Oct 25 '11 at 20:13

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.

share|improve this answer
    
Yes! This worked well and thanks for the explanation. –  crippledlambda Oct 25 '11 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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