AnswerBun.com

Print final spaces of an argument

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 ?

TeX - LaTeX Asked on December 8, 2021

3 Answers

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!

Related Questions

Remove whitespace before and after minted

1  Asked on April 6, 2021 by zusez3

 

how to plot a red dot?

1  Asked on April 6, 2021

     

Vertical spacing in diagram

1  Asked on April 6, 2021

   

How to draw parametric curve in TikZ deltoid and astroid?

2  Asked on April 6, 2021 by esvhei-temvvjin

     

Align equations with the same spacing

2  Asked on April 5, 2021 by sermarfe

       

Adjusting height of left(, right), etc

0  Asked on April 5, 2021 by juan-castao

     

How can I use wxLua in LuaLaTeX on Windows?

0  Asked on April 5, 2021 by slx64

       

Double vertical bar notation

2  Asked on April 5, 2021 by shunjie

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.