Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to have a macro which produces N white spaces if called like this

\mulspc{3} % Produces exactly 3 spaces

\newcommand{\mulspc}[1]{
    \newcounter{ctra}
    \setcounter{ctra}{0}
    \whiledo {\value{ctra} < #1}%
    {
      \hspace*{2mm}
      \stepcounter{ctra}
    }
}

But I have problems that this macro does not produce 0mm spacing in a table cell (just at the beginning of the cell!) if I call zero times \multspc{0}. There is always some indent, but where does this come from?

I need this macro to indent some algorithm in the table cell, its nasty but manualy formating gives me the best result in a longtable, because its multiple pages long...

Thanks for your help!

share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

No need for using a loop:

\newlength\Unit
\setlength\Unit{2mm}
\newcommand\mulspc[1]{\hspace*{#1\Unit}}
        
        
        


foo\mulspc{4}bar
share|improve this answer
    
...now that's clean! Nice! –  Werner Aug 10 '11 at 19:26
add comment

eTeX is always available nowadays, thus you can simply use

\newcommand\mulspc[1]{\hspace*{#1\dimexpr2mm}}
share|improve this answer
add comment

The problem with the current macro is that it unwanted whitespace is added from due to indentation. Add some % add the line ends to get rid of these. However, the current definition also produces a warning after you use \mulspc more than once. That is because the counter definition \newcounter{ctra} is contained within the \newcommand{\mulspc}. Rather define the counter outside the macro definition. Another drawback is that the space offered by \mulspc{0} should be non-existent, which it is not, even if you add % to remove the unwanted spaces. For example, the code:

\documentclass{article}
\usepackage{xifthen}
\newcounter{ctra}%
\newcommand{\mulspc}[1]{
    \setcounter{ctra}{0}%
    \whiledo {\value{ctra} < #1}%
    {\hspace*{2mm}\stepcounter{ctra}}%
}
\begin{document}

\fbox{\mulspc{0}}% Zero spaces

\fbox{}% Empty \fbox

\fbox{\mulspc{1}} % One space

\fbox{\mulspc{3}} % Produces exactly 3 spaces

\end{document}

produces:

Unwanted spacing in \mulspc

To get rid of this spacing, you could use a condition on the parameter passed to \mulspc:

\newcommand{\mulspc}[1]{
  \ifthenelse{#1>0}{%
    \setcounter{ctra}{0}%
    \whiledo {\value{ctra} < #1}%
    {\hspace*{2mm}\stepcounter{ctra}}%
  }{\unskip}%
}

providing \unskip if there is a parameter passed with value less than 1. This produces:

Unwanted spaces gone from \mulspc


Another alternative would be to use the easy interface of the multido package, and define (say)

\usepackage{multido}% A loop facility for Generic TeX
...
\newcommand{\Mulspc}[1]{%
  \multido{\iA=0+1}{#1}{\hspace*{2mm}}%
}

that you can use in a similar way, and is a little cleaner code-wise.

share|improve this answer
1  
That is not because of indentation. Even if you start every line from the first column, a space is still added because of the newline. –  Tsuyoshi Ito Aug 10 '11 at 20:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.