TransWikia.com

Vertically stack text from variable

TeX - LaTeX Asked by Holt on May 30, 2021

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
protecteddefvvv#1{leavevmodebgroupvboxbgroupxvvv#1relax}
defxvvv{afterassignmentxxvvvlettmp= }
defxxvvv{%
ifxtmp@sptokenegroup vboxbgroupletnextxvvv
elseifxtmprelaxegroupegroupletnextrelax
else
hbox to 1.1em{hfilltmphfill}% centred
letnextxvvvfifi
next}
makeatother

newcommand{myvar}{17}

begin{document}

Longstack{p a g e {} myvar{}}
vvv{pagemyvar{}}

end{document}

2 Answers

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

documentclass{article}

protecteddefvvv#1{%
  leavevmode
  bgroupvboxbgroup
  expandafterxvvvexpanded{#1}relax
}
defxvvv{afterassignmentxxvvvlettmp= }
defxxvvv{%
  ifxtmprelax
    egroupegroupletnextrelax
  else
    hbox to 1.1em{hsstmphss}% centred
    letnextxvvv
  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}

Correct answer by egreg on May 30, 2021

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}

newcommandxLongstack[2][c]{%
  resettokcycle%
  Characterdirective{addcytoks{ ##1}}%
  expandedtokcyclexpress{empty#2}%
  deftmpB{Longstack[#1]}%
  expandaftertmpBexpandafter{thecytoks}%
}
begin{document}
xLongstack{page myvar{}}
xLongstack{date today}
end{document}

enter image description here

Answered by Steven B. Segletes on May 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP