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 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?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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}
share|improve this answer
1  
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

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.

share|improve this answer
    
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

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.

share|improve this answer

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.