解释
我格式化了摩洛哥肋骨(银行身份证明我需要将 RIB 的每个部分都用一个特定的标签包裹起来。
但是,目前,我必须手动解析 RIB。
平均能量损失
目前我有以下 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}
渲染
其效果如下:
问题
正如您在代码中看到的,我在特定行中手动设置了国家代码、银行代码和城市代码。
问题
那么,我怎样才能通过类似命令来\moroccanrib
解析变量中的前 4 个字符\country
、变量中的第 5 到第 7 个字符\bank
等等?
答案1
您可以使用\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}
答案2
为了多样性,这里提供一个基于LuaLaTeX的解决方案,利用Lua的强大string.sub
功能。
% !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}
答案3
xstring
规定在和之间(含)\StrMid{<string>}{<numA>}{<numB>}[<cmd>]
的商店:<string>
<numA>
<numB>
<cmd>
\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}