4

I want to stack vertically like this

t
e
x
t

1
9

I found various answers that works, either using custom commands or stackengine (e.g. Vertical text (not in table)).

None of these answers works when part of the text is a variable, e.g.

\Longstack{t e x t {} \value}
\vvv{text \value}

Is there a way to make these work? E.g. for Longstack, is there a way to "insert space" between characters in \value so that Longstack parses the string properly?


Full example -

\documentclass{article}

\usepackage{xstring}
\usepackage{stackengine}

\makeatletter
\protected\def\vvv#1{\leavevmode\bgroup\vbox\bgroup\xvvv#1\relax}
\def\xvvv{\afterassignment\xxvvv\let\tmp= }
\def\xxvvv{%
\ifx\tmp\@sptoken\egroup\ \vbox\bgroup\let\next\xvvv
\else\ifx\tmp\relax\egroup\egroup\let\next\relax
\else
\hbox to 1.1em{\hfill\tmp\hfill}% centred
\let\next\xvvv\fi\fi
\next}
\makeatother

\newcommand{\myvar}{17}

\begin{document}

\Longstack{p a g e {} \myvar{}}
\vvv{page\myvar{}}

\end{document}
4
  • It is a very bad idea to do \def\value{...}.
    – egreg
    Commented Feb 18, 2021 at 18:24
  • @egreg That's not the actual name of the command.
    – Holt
    Commented Feb 18, 2021 at 18:25
  • Please, make a full example showing the issue. We can't know what you're doing
    – egreg
    Commented Feb 18, 2021 at 18:26
  • @egreg Added the example.
    – Holt
    Commented Feb 18, 2021 at 18:28

2 Answers 2

3

You want to expand the variable, or it will be taken as \tmp without expansion. The check for a space is useless.

\documentclass{article}

\protected\def\vvv#1{%
  \leavevmode
  \bgroup\vbox\bgroup
  \expandafter\xvvv\expanded{#1}\relax
}
\def\xvvv{\afterassignment\xxvvv\let\tmp= }
\def\xxvvv{%
  \ifx\tmp\relax
    \egroup\egroup\let\next\relax
  \else
    \hbox to 1.1em{\hss\tmp\hss}% centred
    \let\next\xvvv
  \fi
  \next
}

\newcommand{\myvar}{17}

\begin{document}

\vvv{page \myvar}

\end{document}

enter image description here

Don't use {} to terminate control sequences, as it will add an object to your list.

An expl3 implementation:

\documentclass{article}

\ExplSyntaxOn
\NewDocumentCommand{\vvv}{m}
 {
  \holt_vvv:e { #1 }
 }

\tl_new:N \l__holt_vvv_tl
\seq_new:N \l__holt_vvv_seq

\cs_new_protected:Nn \holt_vvv:n
 {
  \tl_set:Nn \l__holt_vvv_tl { #1 }
  \tl_replace_all:Nnn \l__holt_vvv_tl { ~ } { \scan_stop: }
  \seq_set_split:NnV \l__holt_vvv_seq { } \l__holt_vvv_tl
  \begin{tabular}[b]{@{}c@{}}
  \seq_use:Nn \l__holt_vvv_seq { \\ }
  \end{tabular}
 }
\cs_generate_variant:Nn \holt_vvv:n { e }

\ExplSyntaxOff

\newcommand{\myvar}{17}

\begin{document}

\vvv{page \myvar}

\end{document}
2

Here, I use tokcycle to both expand the argument to the maximum extent possible (it uses \expanded internally) and to add a space between character tokens. Then I feed that token stream to the \Longstack. I call it \xLongstack.

\documentclass{article}
\usepackage{stackengine}
\usepackage{tokcycle}
\newcommand{\myvar}{17}

\newcommand\xLongstack[2][c]{%
  \resettokcycle%
  \Characterdirective{\addcytoks{ ##1}}%
  \expandedtokcyclexpress{\empty#2}%
  \def\tmpB{\Longstack[#1]}%
  \expandafter\tmpB\expandafter{\the\cytoks}%
}
\begin{document}
\xLongstack{page \myvar{}}
\xLongstack{date \today}
\end{document}

enter image description here

You must log in to answer this question.

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