7

Following MWE does not work:

\documentclass{article}
\usepackage{calc}
\newlength{\charwidth}
\setlength{\charwidth}{\widthof{$-$}}
\begin{document}
\centering $\infty$

\centering \hspace{-\charwidth}$-\infty$

\centering \hspace{-\widthof{$-$}}$-\infty$
\end{document}

\hspace{-\widthof{$-$}} throws following error

! Missing number, treated as zero. <to be read again> \widthof l.10 \centering \hspace{-\widthof{$-$}} $-\infty$ ?

Is there a possibility to insert negative \widthof into \hspace without defining a newlength?

Hotschke
  • 5,300
  • 5
  • 33
  • 63
  • 1
    I think we just forgot \hspace when adding calc support to the rest of the format back when we made 2e. You can use the scratch register 0 if you don't want to define a new length \setlength{\dimen0}{-\widthof{$-$}}\hspace{\dimen0} – David Carlisle Jul 28 '15 at 12:25
  • Just accept that you cannot. calc goes through several hoops in order to get \withof to work inside \setlength, so it is no wonder that it will not work in other everyday circumstances. – daleif Jul 28 '15 at 12:28
  • 1
    @DavidCarlisle: While one add can the support for \widthof to hspace (see http://tex.stackexchange.com/a/99242/2388), the negative -\widthof{..} doesn't work -- neither in the \setlength nor elsewhere. – Ulrike Fischer Jul 28 '15 at 12:32
  • 1
    @UlrikeFischer hmm we should fix calc then :( – David Carlisle Jul 28 '15 at 15:58
  • @DavidCarlisle perhaps the Q's title should be modified because already \hspace{\widthof{$-$}} does not work. –  Aug 29 '15 at 17:50

1 Answers1

0

This answer is valid in TexLive 2019 and 2013, but not in 2016. Use it at your own risk.

I just tweaked @DavidCarlisle answer. Instead of just defining the -widthof{$-$}, I substracted it from zero 0pt-widthof{$-$} (it should yield the same ;))

So here is the way to get negative lengths from the \width_ commands \setlength{\dimen0}{0pt-\widthof{$-$}}\hspace{\dimen0}

So your code would look like that

\documentclass{article}
\usepackage{calc}
\newlength{\charwidth}
\setlength{\charwidth}{\widthof{$-$}}
\begin{document}
   \centering $\infty$

\centering \hspace{-\charwidth}$-\infty$

\centering \setlength{\dimen0}{0pt-\widthof{$-$}}\hspace{\dimen0}$-\infty$ \end{document}

Technically no new length is defined as David pointed out, although the scratch register is used.

God bless you, guys.

Edition: result image added

enter image description here

  • The output doesn't match the expected output. – Werner Aug 10 '16 at 18:47
  • Hey @Werner, I added the picture of my output (TeXstudio 2.11.0 + TeX 3.1415926 (TeX Live 2013) kpathsea version 6.1.1 Copyright 2013 D.E. Knuth.) I think everything is ok. :) – loved.by.Jesus Aug 11 '16 at 12:53
  • If you update to TeX Live 2016, the output doesn't match what you show. It may store historic value, but not for current distributions. – Werner Aug 11 '16 at 15:18
  • Well, then it is a 'historic answer', only valid for TeX Live 2013. :\ – loved.by.Jesus Aug 11 '16 at 15:21
  • 1
    It worked for me on TeX Live 2019 in the normal environment (not math, didn’t tried though). – Archange Jul 11 '19 at 08:57
  • @Archange Glad to hear that someone could benefit of my answer! Though it seemed to be obsolete (it got twice -1 ;)) at last it worked for good. – loved.by.Jesus Jul 12 '19 at 14:28