1

Basic loop in latex with variable counter. I want to insert x amount of spaces...

\newcommand{\insertspace}[1]{
     \newcount\foo
     \foo={#1}
     \loop
         ~ 
         \advance \foo -1
    \ifnum \foo>0
    \repeat
}

This call gives me an error: Missing number, treated as zero. I assume this is from the foo counter not recognizing the parameter. (The drawbox is defined in the MWE)

\drawbox{black!100}{5.2cm}{\insertspace{7} Hi im box}

Result:

Result

The insertspace loop works if the counter is static. It also displays my parameter when I reference it somewhere else. Is this something to do with data types?

MWE:

\documentclass{article}

\usepackage{tikz}                                                           
\usetikzlibrary{positioning} 

\begin{document}

    \newcommand{\insertspace}[1]{
        \newcount\foo
        \foo={#1}
        \loop
            ~~ hi 
             \advance \foo -1
        \ifnum \foo>0
        \repeat
        }

    \newcommand{\drawbox}[3]{ % 1->color ---- 2 -> width of box ----- 3-> text
        \begin{tikzpicture}
            \node[draw, fill={#1},rectangle,text width={#2}, text = white] {\color{white}\fontsize{6pt}{6pt}\selectfont #3};
        \end{tikzpicture}
    }

    \drawbox{black!100}{5.2cm}{\insertspace{7} Hi im box}

\end{document}

3 Answers 3

1

You almost did it.
Only a few modifications are needed to get the thing working:

\documentclass{article}

\usepackage{tikz}                                                           
\usetikzlibrary{positioning} 

\newcount\foo

\newcommand{\insertspace}[1]{%
    \foo=#1\relax
    \loop
       ~%
      \advance\foo-1 %
    \ifnum \foo>0 %
    \repeat
    \ignorespaces
}%

\newcommand{\drawbox}[3]{% 1->color ---- 2 -> width of box ----- 3-> text
    \begin{tikzpicture}
    \node[draw, fill={#1},rectangle,text width={#2}, text = white] {\color{white}\fontsize{6pt}{6pt}\selectfont #3};
    \end{tikzpicture}%
}%

\begin{document}
    \drawbox{black!100}{5.2cm}{\insertspace{7}Hi im box}

    \drawbox{black!100}{5.2cm}{\insertspace{7} Hi im box}
\end{document}

Perhaps you are interested in a variant which does without \count-register:

\documentclass{article}

\usepackage{tikz}                                                           
\usetikzlibrary{positioning} 

\newcommand{\insertspace}[1]{%
    \expandafter\insertspaceloop\romannumeral\number\number#1 000\relax
}%
\newcommand{\insertspaceloop}[1]{%
    \ifx\relax#1\ignorespaces\else~\expandafter\insertspaceloop\fi
}%
\newcommand{\drawbox}[3]{% 1->color ---- 2 -> width of box ----- 3-> text
    \begin{tikzpicture}
    \node[draw, fill={#1},rectangle,text width={#2}, text = white] {\color{white}\fontsize{6pt}{6pt}\selectfont #3};
    \end{tikzpicture}%
}%

\begin{document}
    \drawbox{black!100}{5.2cm}{\insertspace{7}Hi im box}

    \drawbox{black!100}{5.2cm}{\insertspace{7} Hi im box}
\end{document}
3

Syntax for low-level TeX counters is \foo <optional equal> <number> <space or \relax> and no braces (it is not an argument). But you shouldn't put \newcount into your definition but outside. Otherwise you will allocate a register each time you call your macro.

2
  • The number I'm trying to use is a parameter. Is this possible via newcommand? Plus I assume I want to create a new counter each time I call the command, is that incorrect?
    – tblev
    Commented May 15, 2020 at 19:40
  • 1
    @tblev the number of available registers is limited (not so much these says but anyway). Allocation in TeX is not scoped for them. so if you put \newcount inside your definition it you allocate \foo over and over again each time taking up a new register (making the old one inaccessible. This is why you should allocate it only once and then just use it in your definition (it is not like a local variable in other languages) giving it a new value but not allocate a new register. Commented May 16, 2020 at 9:29
2

Do you need a loop?

Trick: the normal interword space in the current font is available as \fontdimen2\font.

With the following code you can also say \insertspaces{2.5}, if you want, or even \insertspaces{-3}.

\documentclass{article}

\usepackage{tikz}                                                           
\usetikzlibrary{positioning} 

\newcommand{\insertspace}[1]{\hspace*{#1\fontdimen2\font}\ignorespaces}

\newcommand{\drawbox}[3]{% 1->color ---- 2 -> width of box ----- 3-> text
  \begin{tikzpicture}
  \node[draw, fill={#1},rectangle,text width={#2}, text = white]
    {\color{white}\fontsize{6pt}{6pt}\selectfont #3};
  \end{tikzpicture}%
}

\begin{document}

\drawbox{black!100}{5.2cm}{\insertspace{0} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{1} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{2} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{3} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{4} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{5} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{6} Hi im box}

\drawbox{black!100}{5.2cm}{\insertspace{7} Hi im box}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .