# Using \newcommand to format numbers/strings according to a pattern [duplicate]

This question already has an answer here:

I'm an aspiring LaTeX user and have so far managed the first steps and created my first few documents. Coming from a programming background I've been wondering how you could use \newcommand to create a command that formats numbers or strings in a hard-coded way.

For example, let's say I have the string AB1234567890. Now what I'd like to do is create a command, that used like this

\applyformat{AB1234567890}


should create the output

AB12 3456 7890


I realize something like that can be done with \,

AB12\,3456\,7890


but I would like to define a fixed "layout" in the command to make sure that every string gets formated the same way as I use that command with each string later.

In a programming language I could split strings and such but I'm unaware how to solve that with LaTeX.

## marked as duplicate by egreg, Peter Grill, Werner, Martin Schröder, ChrisSFeb 19 '14 at 23:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• Are those strings always twelve character long? – egreg Feb 19 '14 at 22:43
• In your example, do you want a space every four character from the left or from the right? Generally, you need to write a macro for every different format. It is usually possible if the desired format is specified precisely enough, but there are so many different possibilities that little can be said in general. – Dan Feb 19 '14 at 22:47
• Yes, the strings are always the same length. The counting should begin from the left, however the space is not necessarily always after 4 chars. – LumenAlbum Feb 22 '14 at 23:46

## 1 Answer

You could use the xstring package:

## Notes:

• An enhanced version is available at putting spaces within text block.
• There is a limit in this code of 400 characters, so please adjust the ending value of the \foreach as desired.
• By default the split is done from the left. If you desire the split to be from the right, you need to pass in the first optional parameter with something other than L.

## Code:

\documentclass{article}
\usepackage{xstring}
\usepackage{pgf,pgffor}

\newcommand*{\StringLength}{}%
\newcommand*{\FirstOutputStringLength}{}%
\newcommand*{\TempStringA}{}%
\newcommand*{\TempStringB}{}%
\newcommand*{\applyformat}[2][L]{%
\edef\TempStringA{#2}%
\IfStrEq{#1}{L}{%
\foreach \x in {1,...,100} {%
\StrLeft{\TempStringA}{4}\,%
\StrGobbleLeft{\TempStringA}{4}[\TempStringB]%
\xdef\TempStringA{\TempStringB}%
\IfStrEq{\TempStringA}{}{\breakforeach}{}%
}%
}{%
\StrLen{\TempStringA}[\StringLength]%
\IfEq{\StringLength}{0}{}{%
\pgfmathtruncatemacro{\FirstOutputStringLength}{mod(\StringLength,4)}%
\IfEq{\FirstOutputStringLength}{0}{%
\xdef\TempStringB{\TempStringA}%
}{%
\StrLeft{\TempStringA}{\FirstOutputStringLength}\,%
\StrGobbleLeft{\TempStringA}{\FirstOutputStringLength}[\TempStringB]%
}%
}%
\applyformat[L]{\TempStringB}%
}%
}
\begin{document}
\begin{tabular}{ll}
\multicolumn{1}{c}{Left} & \multicolumn{1}{c}{Right} \\
\applyformat{AB1234567890}   & \applyformat[R]{AB1234567890}\\
\applyformat{AB123456789}    & \applyformat[R]{AB123456789}\\
\applyformat{AB12345678}     & \applyformat[R]{AB12345678}\\
\applyformat{AB1234567}      & \applyformat[R]{AB1234567}\\
\applyformat{AB12}           & \applyformat[R]{AB12}\\
\applyformat{AB}             & \applyformat[R]{AB}\\
\applyformat{A}              & \applyformat[R]{A}\\
\end{tabular}
\end{document}

• This looks like it's exactly what I need. Thank you! I'm surprised that this requires such an elaborate solution In LaTeX, though. There's a lot I need to learn – LumenAlbum Feb 22 '14 at 23:52