4

I am basically reasking the same question as here, which hasn't had any definitive answer even yet.

I restate the problem. The MWE

\documentclass{book}

\usepackage{calc}

\begin{document} \hspace{-\widthof{abc}} \end{document}

produces the following errors:

6: Missing number, treated as zero. \hspace{-\widthof{abc}}
6: Illegal unit of measure (pt inserted). \hspace{-\widthof{abc}}
6: Package calc Error: `\widthof ' invalid at this point. \hspace{-\widthof{abc}}

Questions:

  1. \hspace surely does take negative values and \widthof does produce lengths, so why doesn't it work?
  2. Is there a way to achieve what is intended without resorting to the tedious \newlength and setlength?
Atom
  • 665
  • calc needs to parse for the \widthof and then alters the way \hspace works (there is no expandable way to get the width of arbitrary text, afaik), and seemingly calc didn't implement -\widthof in its parser, so not much you can do about it, other than using \newlength\mytmplength\newcommand\hspaceNegWd[1]{\settowidth\mytmplength{#1}\hspace{-\wd\mytmplength}}. – Skillmon Jan 25 '24 at 07:40
  • 1
    Of course, it's easier if you say \makebox[0pt][l]{abc}, instead of backing up by the width. – egreg Jan 25 '24 at 11:24

1 Answers1

6

The answer is contained in the referenced duplicate but somewhat hidden as at the time that question was asked \hspace did not support calc at all, so even \hspace{1pt+2pt} would have failed.

Unary minus is not implemented for \widthof however infix subtraction is, so you can subtract from 0pt:

enter image description here

\documentclass{book}

\usepackage{calc}

\begin{document}

abc\hspace{0pt-\widthof{abc}}xyz

\end{document}

David Carlisle
  • 757,742