# Typeset a series of characters with equal distances?

for typesetting tables with design of experiments, I have to typeset strings like "+ - + +", - - - +", etc. in a column of a table to represent the so called pattern of the experimental run (sometimes they also contain "a", "A" and "0" (zero)).

However, the width of the "+" and "-" sign are very different in the used font (standard font in KOMA script class scrreprt) and a sequence of 2 or 3 dashes creates one long or very long dash....

So I'm searching for a way to easily typeset e. g. "+ - - - + -" with a little spacing between the characters and (if possible) that several different strings composed of the same number of characters ("+" or "-", "a", "A" and "0") would be aligned with each other, when they are in the same column of a table.

I hope I could make clear what I need, some examples how it shall NOT look are found here:
in line 9 it should be "+ - -" and in 10 "- - +", line 5 should be "- - -"

It looks a little better, if I insert \, between the characters, but as the widths are different, it is still not aligned:

Sure, I could split it up in centered columns like - & + & - & - & ..., but the tables are already complicated enough and I do not want to add 3 to 7 columns...

Does anybody have an idea how to solve that? Maybe a command that parses the string like --+--++ and typesets it with a "constant spacing"?

------- edit:

added math mode for "+" and "-", looks better now, but not perfect:

    \documentclass[11pt, a4paper%,  halfparskip
]{scrreprt}

\usepackage{booktabs}

\begin{document}

\begin{tabular}{cccccccc}
\midrule
1 &        $+$$+$$-$           \\
2 &        $+$$-$$+$           \\
3 &        $-$$+$$+$           \\
4 &        000                 \\
5 &        $-$$-$$-$           \\
6 &        000                 \\
\midrule
7 &        $-$$+$$-$           \\
8 &        000                 \\
9 &        $+$$-$$-$           \\
10 &        $-$$-$$+$           \\
11 &        $+$$+$$+$           \\
12 &        000                 \\
\midrule
13 &        000                 \\
14 &        00a                 \\
15 &        00A                 \\
16 &        0A0                 \\
17 &        A00                 \\
18 &        0a0                 \\
19 &        a00                 \\
20 &        000                 \\
\toprule
\end{tabular}

\end{document}


example and further question about side-effects:

I used the solution of Michel and it looks good, but there is one thing I'm not sure about: Is it possible that it changes the free space between columns? I have the impression that the first 3 columns are closer together than the rest... How can I fix that?

• \texttt{++----++} – Seamus Mar 30 '11 at 13:39

\documentclass{minimal}
\begin{document}
\setlength{\parindent}{0pt}
\newlength{\stretchlen}\setlength{\stretchlen}{1em}
\def\splitterm{\_}
\newcommand{\stretchit}[1]{\leavevmode\realstretch#1\_}
\def\realstretch#1{%
\def\temp{#1}%
\ifx\temp\splitterm
\else
\hbox to \stretchlen{\hss#1\hss}\expandafter\realstretch
\fi}
\stretchit{abcd}\par
\stretchit{----}\par
\stretchit{++++}\par
\stretchit{-+-+}\par
\stretchit{0aA+}\par
\end{document}


\stretchit appends an underscore to its argument and passes it to \realstretch, which recursively 'eats' one letter at a time, and typesets it centered in an \hbox stretched to \stretchlen. All letters are thus equally wide, and since they are centered, aligned nicely.

--- solution for the question:

\documentclass[10pt, a4paper    ]{scrreprt}

\usepackage{booktabs}
\usepackage{collcell}
\usepackage{array}
\begin{document}

\setlength{\parindent}{0pt}
\newlength{\stretchlen}\setlength{\stretchlen}{1em}
\def\splitterm{\_}
\newcommand{\stretchit}[1]{\leavevmode\realstretch#1\_}
\def\realstretch#1{%
\def\temp{#1}%
\ifx\temp\splitterm
\else
\hbox to \stretchlen{\hss#1\hss}\expandafter\realstretch
\fi}

without the stretchit command

\begin{tabular}{cccccccc}
\midrule
1 &        ++-             \\
2 &        +-+             \\
3 &        -++             \\
4 &        000                  \\
5 &        ---                 \\
6 &       000               \\
\midrule
7 &        -+-                 \\
8 &       000                \\
9 &        +--                 \\
10 &        --+             \\
11 &        +++             \\
12 &        000             \\
\midrule
13 &        000                  \\
14 &        00a                   \\
15 &        00A                   \\
16 &        0A0                 \\
17 &        A00                   \\
18 &        0a0                  \\
19 &        a00                  \\
20 &        000                  \\
\toprule
\end{tabular}

\medskip

\emph{with} the stretchit command

\begin{tabular}{c>{\collectcell\stretchit}c<{\endcollectcell}ccccccc}
\midrule
1 &        ++-             \\
2 &        +-+             \\
3 &        -++             \\
4 &        000                  \\
5 &        ---                 \\
6 &       000               \\
\midrule
7 &        -+-                 \\
8 &       000                \\
9 &        +--                 \\
10 &        --+             \\
11 &        +++             \\
12 &        000             \\
\midrule
13 &        000                  \\
14 &        00a                   \\
15 &        00A                   \\
16 &        0A0                 \\
17 &        A00                   \\
18 &        0a0                  \\
19 &        a00                  \\
20 &        000                  \\
\toprule
\end{tabular}

\end{document}

• @Michel: that's it, thank you! Last question: how can I apply that to a whole table column? – MostlyHarmless Mar 30 '11 at 15:21
• @Martin: You can use the collcell package to apply the \stretchit macro to every cell. – Martin Scharrer Mar 30 '11 at 15:44
• I tried >{\stretchit\bgroup}c<{\egroup}, but that gives really weird results. Why doens't it work? – Michel Mar 30 '11 at 15:53
• @Michel: Because macros don't work with implicit braces. Use the collcell package and write >{\collectcell\stretchit}c<{\endcollectcell}. – Martin Scharrer Mar 30 '11 at 15:58
• Okay, thanks, I'll have a look and see why that /does/ work. But, something like >{\realstretch}c<{\_} also behaves oddly. Must be my lack of understanding of the inner workings of tabular ;) – Michel Mar 30 '11 at 16:03

Make sure you typeset them it math-mode, i.e. $-$ and $+$.

Be aware that it makes a difference if you use a single math-mode or separate ones for them. The last sign has different spacing in the first case because it is taken as an unary sign but the others are operators.

Compare the following:

\documentclass{article}

\begin{document}

$-$$-$$-$$+$$+$$+ +$$+$$+$$+$$+$$+$

$-$$-$$-$$-$$-$$-$

$---+++$

$++++++$

$------$

\end{document}


### Result:

For the alignment with the other signs a, A and 0 I would recommend that you make macros for all and set the width manually the one of the widest (should be $+$).

This can be done either with \hbox to <length>{\hss <sign>\hss} (TeX) or using \makebox[<length>][c]{<sign>} (LaTeX). The width is provided by \settowidth{<length register>}{<content>}.

\documentclass[11pt, a4paper]{scrreprt}

\usepackage{booktabs}

\newlength{\slength}
\settowidth{\slength}{$+$}
\newcommand*{\Sz}{\makebox[\slength][c]{0}}
\newcommand*{\SA}{\makebox[\slength][c]{A}}
\newcommand*{\Sa}{\makebox[\slength][c]{a}}
\newcommand*{\Sp}{\makebox[\slength][c]{$+$}}
\newcommand*{\Sm}{\makebox[\slength][c]{$-$}}

\begin{document}

\begin{tabular}{cc}
\toprule
1 &  \Sp\Sp\Sm  \\
2 &  \Sp\Sm\Sp  \\
3 &  \Sm\Sp\Sp  \\
4 &  \Sz\Sz\Sz  \\
5 &  \Sm\Sm\Sm  \\
6 &  \Sz\Sz\Sz  \\
\midrule
7 &  \Sm\Sp\Sm  \\
8 &  \Sz\Sz\Sz  \\
9 &  \Sp\Sm\Sm  \\
10 &  \Sm\Sm\Sp  \\
11 &  \Sp\Sp\Sp  \\
12 &  \Sz\Sz\Sz  \\
\midrule
13 &  \Sz\Sz\Sz  \\
14 &  \Sz\Sz\Sa  \\
15 &  \Sz\Sz\SA  \\
16 &  \Sz\SA\Sz  \\
17 &  \SA\Sz\Sz  \\
18 &  \Sz\Sa\Sz  \\
19 &  \Sa\Sz\Sz  \\
20 &  \Sz\Sz\Sz  \\
\bottomrule
\end{tabular}

\end{document}


### Result:

• Looking at the table, I think this is better than my suggestion... – Seamus Mar 30 '11 at 13:42
• @Martin Scharrer: I don't understand your point about the "unary sign", but I see that in the last 3 rows of your example the block with the last 2 "columns" has a little more space on its left side.. – MostlyHarmless Mar 30 '11 at 14:05
• TeX handles -y differently than x-y: In the first case the minus is an unary oparator (affects only one variable) in the second a binary operator. This affects the spacing. You get the correct spacing for the last symbol in the bottoms examples if you use a brace pair: $---+++{}$ and so makes it a binary symbol. – Ulrike Fischer Mar 30 '11 at 14:06
• @Martin: Ok, now I get it. You can set them manually to have the same width. See my updated answer. – Martin Scharrer Mar 30 '11 at 15:14
• @Martin: Ok, I didn't saw Michel's solution before I updated my question. Both use similar underlying code, just Michel's boxes the characters automatically. – Martin Scharrer Mar 30 '11 at 15:42

one possible form to typeset your table would be to use a table column for each character:

\documentclass[11pt, a4paper]{scrreprt}
\usepackage{booktabs}

\newcommand\Pl{${}+{}$}
\newcommand\Mi{${}-{}$}

\begin{document}

\begin{tabular}{cc@{\hspace{-2pt}}c@{\hspace{-2pt}}c}
\toprule
1 & \Pl & \Pl & \Mi \\
4 & 0 & 0 & 0 \\
12 & a & 0 & A \\
15 & 0 & 0 & A \\
\bottomrule
\end{tabular}

\end{document}


• yes, that's right. However, as I said: "the tables are already complicated enough and I do not want to add 3 to 7 columns..." – MostlyHarmless Mar 30 '11 at 15:37

Michel's solution is nice, but I'd like to present some improvements.

\newlength{\stretchlen}
\settowidth{\stretchlen}{+}
\newcommand{\stretchit}[1]{\realstretch#1\_}
\newcommand\realstretch[1]{%
\ifx#1\_%
\else
\makebox[\stretchlen]{\ifx#1-$-$\else#1\fi}%
%\hbox to \stretchlen{\hss\ifx#1-$-$\else#1\fi\hss}%
\expandafter\realstretch
\fi}


It's not necessary to have \splitterm and to define \temp at each stage, since we're collecting tokens one by one and those tokens are simple characters. It's best also to set \stretchlen to the widest character, in this case +. Finally, there's the particular case of - that should be treated differently with a $-$.

I've left commented the line with \hbox: with \makebox it's more in the LaTeX way of thinking; the two lines give the same result, but of course \makebox is a bit less efficient than \hbox.

Some years later, the solution can be made more robust, with the possibility of managing other characters that need special treatment.

\documentclass[10pt,a4paper]{scrreprt}

\usepackage{booktabs}
\usepackage{collcell}
\usepackage{array}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\stretchit}{m}
{
\tl_map_inline:nn { #1 }
{
\makebox[\stretchlen]
{
\str_case:nnF { ##1 }
{
{-}{$-$}
{+}{$+$}
{*}{$*$}
}
{##1}
}
}
}
\ExplSyntaxOff

\newlength{\stretchlen}
\AtBeginDocument{\settowidth{\stretchlen}{$-$}}

\begin{document}

\begin{tabular}{r >{\collectcell\stretchit}c<{\endcollectcell}}
\midrule
1 & ++- \\
2 & +-+ \\
3 & -++ \\
4 & 000 \\
5 & --- \\
6 & 000 \\
\midrule
7 & -+- \\
8 & -*x \\
9 & +-- \\
10 & --+ \\
11 & +++ \\
12 & 000 \\
\bottomrule
\end{tabular}

\end{document}


An easy way to achieve this would be to typeset the plusses and minuses in a monospace font with \verb|+-+-|

There are however issues with verbatim text which you might run into... Another solution might be to use \texttt and macros for your plusses etc. Here's an example:

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand\p{+}
\newcommand\m{-{}}
\begin{document}
\texttt{++---++}

\texttt{\p\p\m\m\m\p\p}

\texttt{++\m\m aA0}
\end{document}


You need to use a macro like this for the minus sign because otherwise they get turned into en-dashes if you use the fontenc package. This should also mono-space letters easily.

• The problem with -- and --- is that it will be converted to a dash, even with tt font. The \verb should avoid that because it disables ligatures, but has the usual issues of verbatim commands. – Martin Scharrer Mar 30 '11 at 13:47
• @Martin I didn't notice this because tt font only turns -- into a dash if you use fontenc which wasn't in my testing environment – Seamus Mar 30 '11 at 13:54
• Ah, thanks for pointing to fontenc. I wasn't aware of that relationship. – Martin Scharrer Mar 30 '11 at 13:55