Box equivalent to shortstack but allowing to change line spacing
Solution 1
You can do that easily with the stackengine
package. The Shortunderstack
command aligns it at top, and there's an optional argument for the horizontal argument of the contents.
The vertical spacing of line used set with
\setstackgap[S]{some value} (default: 3pt)
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}
\usepackage[usestackEOL]{stackengine}
\begin{document}
She puzzled over this for some time, but at last a bright thought struck
her. ‘Why, it’s a Looking-glass book, of course! And if I hold it up to
a glass, the words will all go the right way again.’
This was the poem that Alice read:
%
\Shortunderstack[l]{Beware the Jabberwock, my son!\\
The jaws that bite, the claws that catch!\\
Beware the Jubjub bird, and shun\\
The frumious Bandersnatch!\\}
‘It seems very pretty,’ she said when she had finished it, ‘but it’s
RATHER hard to understand!’ (You see she didn’t like to confess, even
to herself, that she couldn’t make it out at all.) ‘Somehow it seems
to fill my head with ideas--only I don’t exactly know what they are!
However, SOMEBODY killed SOMETHING: that’s clear, at any rate--’
\end{document}
Solution 2
You can use \shortstack
using \strut
so that it is affected by the baseline, or use tabular
.
\documentclass{article}
\begin{document}
\begin{picture}(10,10)
\put(0,0){x
{\shortstack{a\\b\\C\\1}}
{\shortstack{\strut a\\\strut b\\\strut C\\\strut 2}}
{\linespread{1.5}\selectfont\shortstack[t]{\strut a\\\strut b\\\strut C\\\strut 3}}
{\linespread{2}\selectfont\shortstack[t]{\strut a\\\strut b\\\strut C\\\strut 4}}
{\begin{tabular}[b]{@{}l@{}}a\\b\\C\\5\end{tabular}}
{\renewcommand\arraystretch{1.5}\begin{tabular}[b]{@{}l@{}}a\\b\\C\\6\end{tabular}}
{\renewcommand\arraystretch{2}\begin{tabular}[b]{@{}l@{}}a\\b\\C\\7\end{tabular}}
}
\end{picture}
\end{document}
Solution 3
Here is a \Shotstack
macro that is essentially the same as the usual \shortstack
, but has an additional mandatory argument that is the value of the \baselineskip
for the contents of the stack:
\makeatletter
\gdef\Shortstack{\@ifnextchar[\@Shortstack{\@Shortstack[c]}}
\gdef\@Shortstack[#1]#2{%
\leavevmode
\vbox\bgroup
\baselineskip-\p@\lineskip 3\p@
\let\mb@l\hss\let\mb@r\hss
\expandafter\let\csname mb@#1\endcsname\relax
\let\\\@stackcr\setlength{\baselineskip}{#2}%
\@ishortstack}
\makeatother
Syntax:
\Shortstack[<alignment>]{<baselineskip>}{<contents>}
Example:
\documentclass{article}
\usepackage{lipsum}
\makeatletter
\gdef\Shortstack{\@ifnextchar[\@Shortstack{\@Shortstack[c]}}
\gdef\@Shortstack[#1]#2{%
\leavevmode
\vbox\bgroup
\baselineskip-\p@\lineskip 3\p@
\let\mb@l\hss\let\mb@r\hss
\expandafter\let\csname mb@#1\endcsname\relax
\let\\\@stackcr\setlength{\baselineskip}{#2}%
\@ishortstack}
\makeatother
\begin{document}
\lipsum[1]
\def\MidSeasonEpisodes{Some filler to occupy space}
\Shortstack[l]{1.5em}{\MidSeasonEpisodes\\\MidSeasonEpisodes\\\MidSeasonEpisodes\\\MidSeasonEpisodes}
\lipsum[2]
\end{document}
I don't know why, but the space before and after the \Shortstack
seems messed up... If someone knows, please do tell...
Patrick Storz
Updated on January 11, 2020Comments
-
Patrick Storz almost 4 years
I need a text box that works absolutely equivalent to
\shortstack
, notably- multiline
- has horizontal alignment option
- vertically aligned to top
- does not need me to specify height/width but adjusts automatically to content
but allows me to set the line spacing (unfortunately
\shortstack
specifically does not do that).Is there a suitable macro available?
P.S. I'm in a
picture
environment.-
David Carlisle almost 6 years
tabular
fits all those requirements
-
David Carlisle almost 6 yearsyou are missing a
%
after{#2}
-
Patrick Storz almost 6 years
tabular
is a bit bloated but otherwise works nicely except it seems to add some vertical spacing already with\arraystretch{1}
compared to\shortstack
; is there a workaround for this?\strut
seems to be a very simple and clean solution, however I do not understand the spacing applied - why does\linespread{1}
already produce a lot of space between lines? Isn't\strut
supposed to be1\baselineskip
high? On a side-note:\the\baselineskip
is "-1.0pt" inside a\shortstack
- is there any way to get the "real" value? Then I could also use a simple\vspace*{.5\baselineskip}
-
Patrick Storz almost 6 yearsThanks! Admittedly this is way over my head and a lot more complex than I had imagined, but I certainly will look into it if I find the time. As I do not need this inside flowed text but for precisely positioning single lines in a
picture
environment the messed up space you noted might not be relevant. -
Patrick Storz almost 6 yearsThanks, seems like the most straightforward solution so far with the downside of the additional package requirement. For portability reasons I'd prefer a solution that does not need it but it's not a strict requirement.
-
David Carlisle almost 6 years@EduardBraun in shortstack baseline spacing is disabled, you just get the size of the actual content (which is why
\the\baselineskip
has the value it has and why adding as strut already increases the spacing, as a\strut
is bigger than ana
the current normal value is\normalbaselineskip