# Using loop in newcommand to build longer equal sign

Following Is there a wider equal sign? and "For loop" in newcommand, I have come up with the naive command:

\newcount\tmp

\newcommand{\eqlong}[1][2]{% need this to prevent extra vertical space
=
\tmp=0
\loop
% increment dummy counter
\joinrel=
% repeat the loop provided the counter is within specified bound
\ifnum\tmp<#1
\repeat
}


Supposedly, \eqlong3 for instance should produce =\joinrel=\joinrel=, i.e. an equal sign about 3 times as long as a normal equal sign. The command as I've written doesn't work, but I'm wondering if someone could fix it.

I know a post I linked above (and also Extendible equals sign) mention the extarrows package, but here I just want a symbol whose length I can control with a parameter I pass.

• Your command defines an optional argument delimited by square brackets, so the syntax should be \eqlong[3] (or \eqlong with no argument). See this tutorial on the LaTeX syntax of \newcommand. Oct 31, 2022 at 6:42
• As to the actual command, note that \eqlong[3] actually defines a symbol that is four times as long as = (think about where in the code the loop checks whether or not to terminate). Oct 31, 2022 at 6:48
• % need this to prevent extra vertical space can not be right. It does save one token of memory (as would % after the two =) Oct 31, 2022 at 10:28

Your command defines an optional argument that, if given, must be delimited by square brackets. If I understand your request correctly, you also want \eqlong[n] to produce a symbol n-times the length of =. As given, it produces a symbol (n+1)-times the length of =. This is fixed by simply changing \tmp=0 to \tmp=1.

\documentclass{article}

\newcount\tmp
\newcommand{\eqlong}[1][2]{% need this to prevent extra vertical space
=
\tmp=1
\loop
% increment dummy counter
\joinrel=
% repeat the loop provided the counter is within specified bound
\ifnum\tmp<#1
\repeat
}

\begin{document}
$a=b$

$a\eqlong b$ %default, twice as long

$a\eqlong[3] b$ %three times as long
\end{document}


• ahh, I see I messed that up. I thought there was a deeper issue. My bad.
– D.R
Oct 31, 2022 at 7:49

You can make the command as a one-liner:

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\eqlong}{O{2}}
{
=\prg_replicate:nn { #1 - 1 } { \joinrel= }
}

\ExplSyntaxOff

\begin{document}

$a=b$

$a\eqlong[1] b$% the same as before

$a\eqlong b$% default, twice as long

$a\eqlong[3] b$% three times as long

\end{document}


This of course assumes that the argument you pass is a positive integer. If you pass something else, you'll get an error.

Now let's try and solve a more complicated problem: we want that \eqlong[3] produces a relation symbol exactly three times long as the equal sign.

\documentclass{article}

\makeatletter
\NewDocumentCommand{\eqlong}{O{2}}{%
\mathrel{\mathpalette\eqlong@{#1}}%
}

\NewDocumentCommand{\eqlong@}{mm}{%
\begingroup
\sbox\z@{$\m@th#1=$}%
\ifnum#2>1
\makebox[#2\wd\z@][s]{%
\copy\z@
\kern-0.5\wd\z@
\kern-0.5\wd\z@
$\m@th#1\mkern-8mu$
\copy\z@
}%
\else
\copy\z@
\fi
\endgroup
}
\makeatother

\begin{document}

$a=b$

$a\eqlong[1] b$% the same as before

$a\eqlong b$% default, twice as long

$a\eqlong[3] b$% three times as long

$a\eqlong[9] b$

\sbox0{${=}{=}{=}{=}{=}$}\the\wd0

\sbox0{$\eqlong[5]$}\the\wd0

$\scriptstyle a=b$

$\scriptstyle a\eqlong[1] b$

$\scriptstyle a\eqlong b$

$\scriptstyle a\eqlong[2]b$

\end{document}


As you see, replicating five equals signs has the same width as \eqlong[5].

In OpTeX, you can make the command as a one-liner:

\optdef\eqlong [2]{=\fornum 2..\the\opt \do{\joinrel=}}

$a=b$

$a\eqlong[1] b$% the same as before

$a\eqlong b$% default, twice as long

$a\eqlong[3] b$% three times as long

\bye

\documentclass{article}

\newcount\tmp

\newcommand{\eqlong}[1][2]{%
\tmp=0
\loop
\ifnum\tmp<\numexpr(#1)\relax
\ifnum\tmp>0 \joinrel\fi
=
\repeat
}

\begin{document}

$a\eqlong[0] b$

$a=b$

$a\eqlong[1] b$

$a\eqlong[2] b$

$a\eqlong b$ %default=2

$a\eqlong[3] b$

\end{document}


\documentclass{article}

\newcommand{\eqlong}[1][2]{%
\ifnum#1>0 %
=\ifnum#1>1 \joinrel\fi
\expandafter\eqlong\expandafter[\the\numexpr(#1)-1\expandafter\relax\expandafter]%
\fi
}

\begin{document}

$a\eqlong[0] b$

$a=b$

$a\eqlong[1] b$

$a\eqlong[2] b$

$a\eqlong b$ %default=2

$a\eqlong[3] b$

\end{document}