TransWikia.com

Print final spaces of an argument

TeX - LaTeX Asked on December 8, 2021

I would like to print spaces that are at the end of an argument.

Here is my M(not)WE.

documentclass{article}

newcommandprintspaceinarg[1]{%
    ]texttt{#1}[%
}

setlengthparindent{0em}


begin{document}

printspaceinarg{a     }

printspaceinarg{ab    }

printspaceinarg{abc   }

printspaceinarg{abcd  }

printspaceinarg{abcde }

printspaceinarg{abcdef}

end{document}

The output wanted is the following one.

]a     [

]ab    [

]abc   [

]abcd  [

]abcde [

]abcdef[

Is it possible or do I have to change the API of my macro ?

3 Answers

You can use obeyspaces when the #1 parameter is read:

defprintspaceinarg {bgroupobeyspacesprintspaceinargA}
defprintspaceinargA #1{egroup ]{tt#1}[}

printspaceinarg{a    }

printspaceinarg{ab   }

printspaceinarg{abc  }

printspaceinarg{abcd }

printspaceinarg{abcde}

Answered by wipet on December 8, 2021

I guess you want to state the total length to pad to, better than doing the computations yourself.

documentclass{article}
usepackage{xparse}

ExplSyntaxOn
NewDocumentCommand{padto}{O{0}m}
 {
  ]
  texttt
   {
    #2
    prg_replicate:nn { int_max:nn { #1 - tl_count:n { #2 } } { 0 } } { ~ }
   }
  [
}
ExplSyntaxOff

begin{document}

padto[6]{a}

padto[6]{ab}

padto[6]{abc}

padto[6]{abcd}

padto[6]{abcde}

padto[6]{abcdef}

padto{a}

padto[2]{a}

padto[2]{ab}

end{document}

enter image description here

Answered by egreg on December 8, 2021

Thanks to the advice of David Carlisle, I propose the following solution.

documentclass{article}

newcommandprintspaceinarg[2][0]{%
    ]texttt{#2}kern#1fontcharwdfont`x[%
}

setlengthparindent{0em}


begin{document}

printspaceinarg[5]{a}

printspaceinarg[4]{ab}

printspaceinarg[3]{abc}

printspaceinarg[2]{abcd}

printspaceinarg[1]{abcde}

printspaceinarg   {abcdef}

end{document}

Answered by projetmbc on December 8, 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