8

I am using LaTeX (LuaLaTeX,actually) and lately I found myself doing this a lot:

\vbox{%
    \hbox{\strut foo}%
    \hbox{\strut bar}%
    \hbox{\strut baz}%
}

I use it inside tabular environments, floats, on title pages and sometimes even in normal text. When I do this foo, bar and baz are always a lot shorter than, e.g. \linewidth. I found this to be the easiest and quickest thing to get what I want. On special occasions, when there are a lot of items to display I resort to \tabular{l}.

Both constructs look like this is not how LaTeX is supposed to work from an user's perspective. Is there some environment or anything I can use to achieve the same effect as my 5 lines above do? Of course there are for example all the itemizes, but those all have some margin/padding/indent magic about them that always ends up making me want to use Word. My \vbox-\hbox-\strut construct works always as expected, in 100% of my use cases.

2
  • 2
    \halign{\strut#\crcr foo\cr bar\cr baz\cr} (You can even leave out \strut) Commented May 31, 2016 at 12:20
  • \def\shortycut#1#2#3{\vbox{% \hbox{\strut #1}% \hbox{\strut #2}% \hbox{\strut #3}% } causes an error? Commented May 31, 2016 at 12:21

3 Answers 3

5

Maybe some variety of "long" (rows at fixed baselineskip value) stack would suit your needs.

\documentclass{article}
\usepackage{stackengine}
\setstackEOL{$}
\setstackgap{L}{\normalbaselineskip}
\strutlongstacks{T}
\begin{document}
\begin{tabular}{|l|l|}
\hline
\Longstack[l]{foo$  bar$  baz} & text\\
\hline
\Centerstack[l]{foo$  bar$  baz} & text\\
\hline
\Longunderstack[l]{foo$  bar$  baz} & text\\
\hline
\end{tabular}
\end{document}

enter image description here

2
  • This is the first time I come across stackengine. This looks awesome. especially with the long center and longunder variants for tabular environments!
    – Bananguin
    Commented May 31, 2016 at 12:33
  • @user1129682 Thanks. As it is my package, let me know if you have any questions on its use. Commented May 31, 2016 at 12:42
7

If you want to write your current construct much shorter, use \halign (and consider using plain TeX).

\documentclass{article}
\begin{document}
\halign{#\crcr foo\cr bar\cr baz\cr}
\end{document}

enter image description here

2
  • Does \halign fall under the category of tex primitives we are not supposed to use (like hbox and vbox)?
    – Bananguin
    Commented May 31, 2016 at 12:36
  • 2
    \halign is a TeX primitive, but I have never heard that you are not supposed to use any TeX primitives. It is perfectly safe to use and well documented in the TeXbook. Commented May 31, 2016 at 12:44
5
\input expl3-generic

\ExplSyntaxOn

\cs_new_protected:Npn \vboxofhboxes #1
 {
  \vbox:n
   {
    \clist_map_inline:nn { #1 }
     {
      \hbox:n { \strut ##1 }
     }
   }
 }

\ExplSyntaxOff

\vboxofhboxes{
  foo,
  bar,
  baz,
  OK
}

\bye

enter image description here

I can't really see the advantage over

\begin{tabular}{@{}l@{}}
foo \\
bar \\
baz
\end{tabular}

or similar Plain TeX construction.

2
  • 1
    What is this expl3 sorcery? It looks like a lot of libraries to include which will make the build process very time consuming. The advantage would be that \vboxofhboxes adds a lot less clutter to my documents and of course I can redefine \vboxofhboxes once to add fancy layout everywhere, if I wanted to.
    – Bananguin
    Commented May 31, 2016 at 12:40
  • @user1129682 Time consuming? Not at all, just a couple of seconds, perhaps. On the other hand, it provides for very compact code.
    – egreg
    Commented May 31, 2016 at 18:15

You must log in to answer this question.

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