TeX - LaTeX Asked by MostlyHarmless on December 25, 2020
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?
documentclass{minimal}
begin{document}
setlength{parindent}{0pt}
newlength{stretchlen}setlength{stretchlen}{1em}
defsplitterm{_}
newcommand{stretchit}[1]{leavevmoderealstretch#1_}
defrealstretch#1{%
deftemp{#1}%
ifxtempsplitterm
else
hbox to stretchlen{hss#1hss}expandafterrealstretch
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}
defsplitterm{_}
newcommand{stretchit}[1]{leavevmoderealstretch#1_}
defrealstretch#1{%
deftemp{#1}%
ifxtempsplitterm
else
hbox to stretchlen{hss#1hss}expandafterrealstretch
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>{collectcellstretchit}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}
Correct answer by Michel on December 25, 2020
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}
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 & SpSpSm
2 & SpSmSp
3 & SmSpSp
4 & SzSzSz
5 & SmSmSm
6 & SzSzSz
midrule
7 & SmSpSm
8 & SzSzSz
9 & SpSmSm
10 & SmSmSp
11 & SpSpSp
12 & SzSzSz
midrule
13 & SzSzSz
14 & SzSzSa
15 & SzSzSA
16 & SzSASz
17 & SASzSz
18 & SzSaSz
19 & SaSzSz
20 & SzSzSz
bottomrule
end{tabular}
end{document}
Answered by Martin Scharrer on December 25, 2020
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}
newcommandp{+}
newcommandm{-{}}
begin{document}
texttt{++---++}
texttt{ppmmmpp}
texttt{++mm 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.
Answered by Seamus on December 25, 2020
one possible form to typeset your table would be to use a table column for each character:
documentclass[11pt, a4paper]{scrreprt}
usepackage{booktabs}
newcommandPl{${}+{}$}
newcommandMi{${}-{}$}
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}
Answered by Gonzalo Medina on December 25, 2020
Michel's solution is nice, but I'd like to present some improvements.
newlength{stretchlen}
settowidth{stretchlen}{+}
newcommand{stretchit}[1]{realstretch#1_}
newcommandrealstretch[1]{%
ifx#1_%
else
makebox[stretchlen]{ifx#1-$-$else#1fi}%
%hbox to stretchlen{hssifx#1-$-$else#1fihss}%
expandafterrealstretch
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 >{collectcellstretchit}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}
Answered by egreg on December 25, 2020
A quick and dirty solution might be to enclose the symbols in braces: ${-}{+}{+}{+}$
looks better than $-+++$
, as LaTeX won't try to interpret each +
or -
as a binary operator.
Answered by Antonio on December 25, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP