6

Explanations

I format a moroccan RIB (Relevé d’identité bancaire for “Bank account number”) to display it as a nice rendering. I need to take each part of the RIB to embrace it with a specific label.

But, for the moment, I have to manually parse the RIB.

MWE

For the moment I have the following MWE:

\documentclass{article}
\usepackage[load-configurations = abbreviations]{siunitx}

\newcommand{\textunderbrace}[2]{%
  \ensuremath{\underbrace{\text{#1}}_{\text{#2}}}%
}
\newcommand{\textoverbrace}[2]{%
  \ensuremath{\overbrace{\text{#1}}^{\text{#2}}}%
}


\begin{document}

\textunderbrace{PPPP}{pays} \textoverbrace{\textunderbrace{BBB}{banque} \textunderbrace{VVV}{ville} \textunderbrace{NNNNNNNNNNNNNNNN}{Numéro} \textunderbrace{CC}{clé}}{RIB}

\end{document}

Rendering

Which gives the following rendering:

Rendering of part braced Moroccan RIB

The problem

As you see in the code, I manually set the country code, the bank code, the city code in a specific row.

The question

So, how can I, from a command like \moroccanrib parse the 4 first chars in \country variable, the 5th-7th char in \bank variable, etc?

2
  • 1
    You can also use the xstring package (the manual is readable using acrobat now). Commented Aug 16, 2021 at 18:25
  • It was said that the EU IBAN number was a solution looking for a problem. I think we found one :) Commented Aug 17, 2021 at 23:55

3 Answers 3

6

You can use \tl_range:nnn

\documentclass{article}
\usepackage{amsmath}

\newcommand{\textunderbrace}[2]{%
  \ensuremath{\underbrace{\text{#1}}_{\text{#2\vphantom{Ay}}}}%
}
\newcommand{\textoverbrace}[2]{%
  \ensuremath{\overbrace{\text{#1}}^{\text{#2}}}%
}

\ExplSyntaxOn

\NewDocumentCommand{\rib}{m}
 {
  \textunderbrace{ \tl_range:nnn { #1 } { 1 } { 4 } } {pays}~
  \textoverbrace
   {
    \textunderbrace{ \tl_range:nnn { #1 } { 5 } { 7 } } {banque}~
    \textunderbrace{ \tl_range:nnn { #1 } { 8 } { 10 } } {ville}~
    \textunderbrace{ \tl_range:nnn { #1 } { 11 } { 26 } } {numéro}~
    \textunderbrace{ \tl_range:nnn { #1 } { 27 } { 28 } } {clé}
   }
   { RIB }
 }

\ExplSyntaxOff

\begin{document}

\rib{PPPPBBBVVVNNNNNNNNNNNNNNNNCC}

\end{document}

enter image description here

1
  • Thank you. Both answers are pretty satisfying but as I will make a class with this command, I think your answer is better because it doesn’t call too specific packages.
    – fauve
    Commented Aug 16, 2021 at 17:54
6

For the sake of variety, here's a LuaLaTeX-based solution, which makes use of Lua's powerful string.sub function.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}    % or some other suitable document class
\usepackage[french]{babel} % optional
\usepackage{amsmath}       % for "\ensuremath" macro

%% Define two Lua functions (1 main, 1 aux.)
\directlua{
  %% Auxilliary function
  function surround_s ( t )
    return ( "\\textup{" .. t .. "\\strut}" )
  end
  %% Main function
  function parse_RIB ( s )
    %% Extract the input string's five components
    s1 = surround_s ( s:sub ( 1 , 4 ) )
    s2 = surround_s ( s:sub ( 5 , 7 ) )
    s3 = surround_s ( s:sub ( 8 , 10 ) )
    s4 = surround_s ( s:sub (11 , 26 ) )
    s5 = surround_s ( s:sub (27 , 28 ) )
    %% Print the formatted result
    tex.sprint ( "\\ensuremath{" ..
                 "\\underbrace{" .. s1 .. "}_{\\textup{pays}}"   ..
     "\\overbrace{\\underbrace{" .. s2 .. "}_{\\textup{banque}}" ..
                 "\\underbrace{" .. s3 .. "}_{\\textup{ville}}"  ..
                 "\\underbrace{" .. s4 .. "}_{\\textup{numéro}}" ..
                 "\\underbrace{" .. s5 .. "}_{\\textup{clé}}}"   .. 
     "^{\\textup{RIB}}}" )
  end
}
%% LaTeX wrapper macro for the main Lua function
\newcommand\moroccanrib[1]{\directlua{ parse_RIB ( "#1" ) }}

\begin{document}

%% Set up an RIB
\newcommand\myRIB{PPPPBBBVVVNNNNNNNNNNNNNNNNCC} 

%% Verify that input string has correct length (28)
\directlua{tex.sprint(string.len("\myRIB"))} 

\moroccanrib{\myRIB}
\end{document}
5

xstring provides \StrMid{<string>}{<numA>}{<numB>}[<cmd>] that stores <string> between <numA> and <numB> (inclusive) in <cmd>:

enter image description here

\documentclass{article}

\usepackage{xstring,amsmath}

\newcommand{\moroccanrib}[1]{%
  \StrMid{#1}{1}{4}[\country]%     Extract country
  \StrMid{#1}{5}{7}[\bank]%      Extract bank
  \StrMid{#1}{8}{10}[\city]%     Extract city
  \StrMid{#1}{11}{26}[\ribno]%   Extract number
  \StrMid{#1}{27}{28}[\key]% Extract key
  \textunderbrace{\country}{pays}~%
  \textoverbrace{%
    \textunderbrace{\bank}{banque}
    \textunderbrace{\city}{ville}
    \textunderbrace{\ribno}{Numéro}
    \textunderbrace{\key}{clé}%
  }{RIB}
}

\newcommand{\textunderbrace}[2]{%
  \ensuremath{\underbrace{\text{#1}}_{\text{#2}}}%
}
\newcommand{\textoverbrace}[2]{%
  \ensuremath{\overbrace{\text{#1}}^{\text{#2}}}%
}

\begin{document}

\moroccanrib{PPPPBBBVVVNNNNNNNNNNNNNNNNCC}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .