# Make a length negative

I was wondering if there is a way to have a \negate command which makes lengths negative. So you could do things like:

\newlength{\negativeabovecaptionskip}
\setlength\negativeabovecaptionskip{\negate{\abovecaptionskip}}

\the\abovecaptionskip %prints: 10pt
\the\negativeabovecaptionskip %prints: -10pt

%%But also:
\newlength{\negativeparskip}
\setlength\negativeparskip{\negate{\parskip}}

\the\parskip %prints: 15pt plus 2pt minus 1pt
\the\negativeparskip %prints: -15pt plus 1pt minus 2pt
%(notice that the the plus/minus values switched possitions)


Any ideas?

-

I do find your second requirement a bit curious. Why should the stretch and shrink part be exchange? Aren't you looking simply for the normal minus sign?

\documentclass[parskip]{scrartcl}
\begin{document}
\newlength{\negativeabovecaptionskip}
\setlength\negativeabovecaptionskip{-\abovecaptionskip}

\the\abovecaptionskip %prints: 10pt

\the\negativeabovecaptionskip %prints: -10pt

%%But also:
\newlength{\negativeparskip}
\setlength\negativeparskip{-\parskip}

\the\parskip %prints: 15pt plus 2pt minus 1pt

\the\negativeparskip %prints: -15pt plus 1pt minus 2pt

\end{document}

-
So did I, but decided to take on the challenge of the question as-written! –  Joseph Wright Oct 8 '11 at 13:43
@Ulrike: You are right, just negating all tree numbers achieves the same result I wanted. I didn't know I could just use a minus sign like that. I think I tried it at some point and got an error, but I guess that was due to some bug in my code. It works fine now. Thanks. –  Matthias Oct 9 '11 at 8:54

Using e-TeX, you can do something like

\documentclass{article}
\newcommand*\negate[1]{%
-\dimexpr#1 plus\glueshrink #1 minus \gluestretch #1
}
\begin{document}
\newlength{\negativeabovecaptionskip}
\setlength\negativeabovecaptionskip{\negate{\abovecaptionskip}}

\the\abovecaptionskip %prints: 10pt

\the\negativeabovecaptionskip %prints: -10pt

%%But also:
\parskip 15pt plus 2pt minus 1pt

\newlength{\negativeparskip}
\setlength\negativeparskip{\negate{\parskip}}

\the\parskip %prints: 15pt plus 2pt minus 1pt

\the\negativeparskip %prints: -15pt plus 1pt minus 2pt
%(notice that the the plus/minus values switched positions)
\end{document}


making use of the fact that \dimexpr will turn a skip into a dimension, and that we can extract the stretch and shrink components.

-

Switching the plus and minus parts has hardly a meaning, as Ulrike observes. If you don't insist in this,

\newcommand*{\negate}[1]{\glueexpr -2\glueexpr#1\relax+#1\relax}


Multiplying a glue parameter by a factor will annihilate the stretch and shrink component (and \gluexpr behaves like a glue parameter).

Note that prepending a simple minus sign to a glue parameter changes sign to each component without annihilating the stretch and shrink components, in contrast to prepending -1.

-
Why not just use \dimexpr? –  Joseph Wright Oct 8 '11 at 15:49
With \glueexpr you can directly call \negate{3pt plus 1pt minus 2pt}, which you couldn't with \dimexpr. The argument of \negate might be generated as the expansion of some macro. –  egreg Oct 8 '11 at 15:50