# controling padding using the xstring package

in order to pad some text with a predefined padding, im using the xstrings package

my current code is

\documentclass{minimal}
\usepackage{xstring}

\begin{document}
\begin{tabular}{c|c}
\textbf{use} & \textbf{Expected} \\
\hline\\

\padfrom{0000}{ab} & 00ab \\

\end{tabular}
\end{document}


however that fails to compile (i can only compile the StrGobbleLeft usage when i put a raw number there, using macros/commands does not seem to work

-

It's a rather common problem: \StrLen doesn't produce directly a number, but all the instructions to get at it. Use it like this:

\newcommand\padfrom[2]{\StrLen{#2}[\templen]\StrGobbleLeft{#1}{\templen}#2}


so that \templen really contains only the number representing the string's length.

-

If the reference string (length) is fixed, then there is no need repeating the reference string within the table. More importantly, notice that the result for the entry <space>abc<space> is not what you expected.

\documentclass{article}
\usepackage{xstring}
\usepackage{array,hhline}
\newcommand\fletters[1]{\StrLen{#1}[\templen]\StrGobbleLeft{0000}{\templen}#1}
\def\jline{\\\hhline{*2{|=}|}}

\begin{document}
\begin{tabular}{*2{|>{\centering\arraybackslash}m{3cm}}|}\hline
\textbf{Given} & \textbf{Expected}\jline
\fletters{ abc} & 00ab \\\hline
\fletters{abcde} & \fletters{ab} \\\hline
\fletters{a} & \fletters{ab} \\\hline
\end{tabular}
\end{document}


EDIT

Here is a more robust solution.

\documentclass{article}
\usepackage{catoptions}
\usepackage{array,hhline}
\makeatletter
% \getStringLength[<result.cmd>]{<string>}
% \getStringLength*[<result.cmd>]{<stringcmd>}
% The default of <result.cmd> is \slength.
\robust@def*\getStringLength{\cpt@teststopt\get@StringLength\slength}
\robust@def*\get@StringLength[#1]#2{%
\begingroup
\cpt@stchoose{cpt@st}{#2}\reserved@a\getStringLength
\despacecontent\reserved@a
\@tempcnta\z@pt
\edef\slength{\the\@tempcnta}%
\postgroupdef\slength\endgroup
}
% \stringGobbleLeft[<result.cmd>]{<string>}{<number>}
% \stringGobbleLeft*[<result.cmd>]{<stringcmd>}{<number>}
% The default of <result.cmd> is \rstring.
\robust@def*\stringGobbleLeft{\cpt@teststopt\string@GobbleLeft\rstring}
\robust@def*\string@GobbleLeft[#1]#2#3{%
\begingroup
\cpt@stchoose{cpt@st}{#2}\reserved@a\stringGobbleLeft
\despacecontent\reserved@a
\@tempcnta\z@pt
\def#1{}%
\expandafter\cpttfor\reserved@a\dofor{%
\ifnum\@tempcnta>#3\relax
\edef#1{\expandcsonce#1\unexpanded{##1}}%
\fi
}%
\postgroupdef#1\endgroup
}

% Tests:
\stringGobbleLeft{ 0 0 0 0 }{4}
%\show\rstring % -> empty
% It is possible to take account of unprotected spaces in the string,
% but I am not sure that is what you need.
\stringGobbleLeft{ 0{ }0{ }0 0 }{4}
%\show\rstring % -> 00

\robust@def*\fletters#1{%
\getStringLength{#1}\stringGobbleLeft{0000}\slength
\rstring\cpttrimspace{#1}%
}

\def\jline{\\\hhline{*2{|=}|}}
\makeatother

\begin{document}
\begin{tabular}{*2{|>{\centering\arraybackslash}m{3cm}}|}\hline
\textbf{Given} & \textbf{Expected}\jline
\fletters{ abc } & 00ab \\\hline
\fletters{abcde} & \fletters{ab} \\\hline
\fletters{ a } & \fletters{ab} \\\hline
\end{tabular}
\end{document}

-
this is quite frankly overdoing it this is tonns of code for a problem i don't have – Ronny Nov 7 '12 at 17:22
It isn't about you alone, although you asked the question. Some other visitors will find the discussion here. Some users don’t realize that the site is a repository. – Ahmed Musa Nov 8 '12 at 20:02