1

I am trying to write a command that will do the following, in order:

  1. Measure the width of an input word.
  2. Compare this value to an existing value.
  3. If the input word's width is greater, then take on that measure as value. Otherwise, do nothing.

What is going wrong with the following code?

\documentclass{article}
\usepackage{ifthen}
\usepackage{calc}
\usepackage{showframe}

\newlength\shape \setlength{\shape}{1mm}

\newcommand{\shifter}[1]{\ifthenelse {\shape<\widthof{#1}} % If the width of the input word is greater than the length of \shape, {\setlength{\shape}{\widthof{#1}}} % then make the length of \shape equal to the width of the input word. {} % Otherwise, do nothing. }%

\setlength{\parindent}{0pt}

\begin{document}

\hspace{\shape}Hello % Output as expected: `Hello' is indented 1mm from the left margin.

\shifter{Tyrannosaurus}

\hspace{\shape}Hello % Output not as expected. I wanted Hello' to be indented the width of the wordTyrannosaurus' from the left margin. Instead, `Hello' is indented 1mm from the left margin.

\end{document}

Possibly relevant: I am getting the following error:

A number should have been here; I inserted `0'.

Why?

Noah J
  • 515
  • 2
    Your question sounds a bit like an x-y-problem. Can you add a bit more background what you are actually trying to do? – samcarter_is_at_topanswers.xyz Aug 21 '23 at 20:06
  • 1
    \widthof{#1} is not a length that can be used in ifthenelse, it is a special construct just valid in calc package expressions (which includes \setlength) – David Carlisle Aug 21 '23 at 21:54
  • 1
    @samcarter_is_at_topanswers.xyz Ultimately, I am trying to somewhat alter Zarko's answer (https://tex.stackexchange.com/a/693984/277990) to a previous question I had so that it works without obliging the writer to manually plug in the widest word of a group of words (and find out which one is the widest to begin with). See \settowidth\colwidth{Yessirino} % <--- select the longest text in the last columns in Zarko's code in the above link. (The purpose of \colwidth is to locally establish a column width.) – Noah J Aug 21 '23 at 23:19

2 Answers2

1

You neither need ifthen nor calc. Just define a second dimen for comparison, set it to the width with settowidth (really descriptive) and use a more robust condition of etoolbox:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{showframe}

\newdimen\shape \setlength{\shape}{1mm} \newdimen\shapecompare \newcommand{\shifter}[1]{% \settowidth\shapecompare{#1}% \ifdimless{\shape}{\shapecompare}% {\setlength{\shape}{\shapecompare}}% {}% }%

\setlength{\parindent}{0pt}

\begin{document}

\hspace{\shape}Hello

\shifter{Tyrannosaurus}

Tyrannosaurus

\hspace{\shape}Hello

\end{document}

lukeflo
  • 1,555
0

There are a few problems with your code.

First problem: if you want to compare lengths with \ifthenelse you need \lengthtest (only comparison between integers needs nothing).

Second problem: \widthof doesn't work in \lengthtest.

Fix: use a scratch dimension register.

\documentclass{article}
\usepackage{ifthen}
\usepackage{showframe}

\newlength\shape \setlength{\shape}{1mm}

\newcommand{\shifter}[1]{% \settowidth{\dimen0}{#1}% \ifthenelse{\lengthtest{\shape<\dimen0}}{\setlength{\shape}{\dimen0}}{}% }

\setlength{\parindent}{0pt}

\begin{document}

\hspace{\shape}Hello

\shifter{Tyrannosaurus}

\hspace{\shape}Hello

Tyrannosaurus % check the space

\end{document}

enter image description here

You don't need ifthen, actually:

\newcommand{\shifter}[1]{%
  \settowidth{\dimen0}{#1}%
  \ifdim\shape<\dimen0
    \setlength{\shape}{\dimen0}%
  \fi
}
egreg
  • 1,121,712