Pad operator not doing as implied

Folks,

The pad operator works fine if you want to pad numbers and you could add leading or trailing zeros, even spaces however in more string based uses it fails.

Consider this in a list with the counter item and total count of items

   \function items-size() [<items>length[]]
   \function item-size() [<item>length[]]
   \function item-pad() [<items-size>add[1]subtract<item-size>]
   \function item-padded() [<item>pad:prefix<item-pad>]

<$let items={{{ [tag[TableOfContents]count[]] }}}>

;items=<<items>>, items-size=<<items-size>>, item-pad=<<item-pad>>
<$list filter="[tag[TableOfContents]]" counter=item>
    <<item-padded>> <$link/> item-size=<<item-size>>, <br>
</$list>

</$let>

This prefixes each item with a zero padding.

Now change item-padded to;

   \function item-padded() [<item>addsuffix[.]pad:suffix<item-pad>,[ ]]
  • Idealy this would result in the following;
1.  Home
...
9.  Reference
10. Title
  • That is add 2 spaces for units, zero for tens.

But it does not!

Hi Tony! I really need to learn all this 5.3 stuff!

You don’t need to recalculate the pad size with each item. You just need to calculate the maximum size you want and add “1”. Possibly use the Count widget. So for your first example that would be “3”, and for your second example that would be “4”

In other words, you want something equivalent to:

\function item-padded() [<item>addsuffix[.]pad:suffix[4],[ ]]

But it still won’t work the way you’re hoping because HTML always crushes multiple spaces down to a single space. So possibly you could use a stub character (e.g. &) and then substitute a non-breaking space (&nbsp;) for each stub before rendering.

“Always” may be a little strong here. It doesn’t happen in <pre>-formatted text, nor when a preformatted CSS white-space property is applied. But yes, this is the norm.

Thanks all especially @Mark_S and as @Scott_Sauyet said we can use a special Unicode that is a blank of the requires size,

this is what I am using;

   \function padding() [<items>length[]add[2]]
   \function item-padded() [<item>addsuffix[.]pad: suffix<padding>,[ ]]
   <!-- above padding character is "em Space" Unicode Character “ ” (U+2002)-->

then when listing

<<item-padded>><$link/><br>