15

Is it possible to have some words written over each other in normal text? The use case is that it should be used instead of slashes where a sentence talks about two similar options.

A short example:

\documentclass{article}
\begin{document}
If you walk left/right, you will see a house/boat with an old man/a cat.
\end{document}

I'm hoping to get a result that looks a bit like this mockup (done in an ugly and very manual manner here with \raisebox and \hspace):

mockup of example text

20

This is what the \stackanchor macro does in the stackengine package. I show three instances, center, left, and right aligned. In the last instance, I set the stackgap between the items to 8pt, rather than the default (3pt). Note that the gap can be alternately achieved via \stackanchor[8pt]{top item}{bottom item}.

The default stack sets the gap between the bottom of the top item and the top of the bottom item (what I call short stacks). Alternately, you could set the gap between baselines of the two stacked items (what I call long stacks) with \def\stacktype{L}and \setstackgap{L}{inter-baseline gap}.

\documentclass{article}
\usepackage{stackengine}
\parskip 1em
\begin{document}
\def\mytext{If you walk \stackanchor{left}{right}, you will see a
\stackanchor{house}{boat} with \stackanchor{an old man}{a cat}.}

\mytext

\def\stackalignment{l}\mytext

\setstackgap{S}{8pt}
\def\stackalignment{r}\mytext
\end{document}

enter image description here

Alternately, \stackon and \stackunder will produce the stack where the baseline is not split, but instead aligned with either the lower item (\stackon) or upper item{\stackunder), respectively.

  • Wow, that's a powerful package. – quazgar Aug 30 '13 at 19:39
  • totally agree with quazgar—it makes it so simple! – loved.by.Jesus Feb 18 '16 at 16:09
  • What if I want to stack three or more lines of text? – Adriaan Apr 17 '18 at 15:08
  • @Adriaan Then there are other options, such as \Shortstack, \Longstack, \Shortunderstack, \Lonunderstack, and \Centerstack. If the argument needs horizontal tabbing, in addition to vertical stacking, then the tabstackengine package extends the syntax of the stackengine package. – Steven B. Segletes Apr 17 '18 at 21:38
6

If you're interested in that particular syntax, the following might be of interest:

enter image description here

\documentclass{article}
\makeatletter
\def\dual#1{\expandafter\dual@aux#1\@nil}
\def\dual@aux#1/#2\@nil{\begin{tabular}{@{}c@{}}#1\\#2\end{tabular}}
\makeatother
\begin{document}
If you walk \dual{left/right}, you will see a \dual{house/boat} with \dual{an old man/a cat}.
\end{document}

It uses the parameter text of a \definition to specify the required input: \dual{<top>/<bottom>}. If you wish to have a different layout (not vertically centred), try using \begin{tabular}[t]:

enter image description here

or \begin{tabular}[b]:

enter image description here


Here is an expansion of the above solution that provides some key-value options:

enter image description here

\documentclass{article}
\usepackage{xkeyval}% http://ctan.org/pkg/xkeyval
\makeatletter
\newlength{\dk@toplen}\newlength{\dk@botlen}
\define@cmdkey{dualkey}[dk@]{halign}[c]{}%
\define@cmdkey{dualkey}[dk@]{valign}[c]{}%
\define@key{dualkey}{topwidth}{\setlength{\dk@toplen}{#1}}
\define@key{dualkey}{botwidth}{\setlength{\dk@botlen}{#1}}
\newcommand{\dual}[2][,]{% \dual[<opts>]{<top>/<bot>}
  \setkeys{dualkey}{valign=c,halign=c,topwidth=\m@ne\p@,botwidth=\m@ne\p@,#1}% Set (default) keys
  \expandafter\dual@aux#2\@nil}% Print dual
\def\dual@aux#1/#2\@nil{%
  \begin{tabular}[\dk@valign]{@{}\dk@halign @{}}
    \ifdim\dk@toplen<\z@ #1\else\makebox[\dk@toplen]{#1}\fi\\
    \ifdim\dk@botlen<\z@ #2\else\makebox[\dk@botlen]{#2}\fi
  \end{tabular}}
\makeatother
\begin{document}
If you walk \dual[valign=b]{left/right}, you will see a \dual[valign=b,topwidth=0pt]{house/boat} with 
\dual[valign=t,halign=r]{an old man/a cat}.
\end{document}

You can now set the vertical and horizontal alignment within \dual (default is center for both), as well as the width of the respective items. This allows for a better flow of the sentence structure, if need be. Line-breaking at the end of the text block will still be problematic though, due to the boxing nature of \dual.

  • I'd prefer \dual{left}{right} that's more easily manageable. – egreg Aug 30 '13 at 19:11
4

What that is, eventually, is a vertical box which has horizontal boxes inside, so (in plain-tex; compile with tex/pdftex/xetex/luatex):

\catcode`/=13
\def/#1/#2/{\leavevmode$\vcenter{\hbox{#1}\hbox{#2}}$}
If you walk , you will see a /left/right/, you will see a /house/boat/ with an
old /man/a cat/.
\bye

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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