\documentclass[a4paper,10pt]{article} \usepackage{xstring} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage[dvips,colorlinks=true,hyperfootnotes=false,citecolor=black,filecolor=black,linkcolor=blue,urlcolor=blue,bookmarks=false,pdfauthor={Christian Tellechea},pdftitle={xstring},pdfsubject={package for strings},pdfkeywords={xstring,latex,string},pdfcreator={LaTeX}]{hyperref} \usepackage[a4paper,dvips,margin=2cm]{geometry} \usepackage{amsmath,amssymb,amsfonts,textcomp} \usepackage{amssymb} \usepackage{moreverb} \usepackage{lmodern} \usepackage{eurosym} \usepackage{xstring} \usepackage[french,english]{babel} \makeatletter \newcommand*\Exemple{\@ifstar{\def\frontiere{|}\@Ex}{\def\frontiere{}\@Ex}} % la macro de Ulrich Diez \newcommand*\@Ex{% \relax% \ifmmode\hbox\else\leavevmode\null\fi% \bgroup% \verb@eol@error% \let\do\@makeother\dospecials% \verbatim@font\@noligs% \@@Ex} \newcommand*\@@Ex[1]{% \@vobeyspaces\frenchspacing\ \catcode`#1\active% \lccode`\~`#1% \gdef\verb@balance@group{\@ehc}% \aftergroup\verb@balance@group% \lowercase{% \let ~\verb@egroup% \def\@tempa##1~{\hbox to \Partage\linewidth{\hfil##1~}\quad\scantokens{\frontiere##1\frontiere}\par}}% \@tempa}% \makeatother \newcommand\guill[1]{"#1"} \newcommand\argu[1]{$\langle$\textit{#1}$\rangle$} \newcommand\ARGU[1]{\texttt{\{}\argu{#1}\texttt{\}}} \newcommand\arguC[1]{\texttt{[}\argu{#1}\texttt{]}} \newcommand\arguCC[2]{\texttt{[}\argu{#1}{,}\argu{#2}\texttt{]}} \newcommand\texte[1]{\texttt{text}${}_{#1}$} \newcommand\voirdeftexte{~(see \ref{deftexte})} \newenvironment{Conditions}[1][1cm]% {\begin{list}% {$\vartriangleright$}% {\setlength{\leftmargin}{#1} \setlength{\itemsep}{0pt} \setlength{\parsep}{0pt} \setlength{\topsep}{2ptplus3ptminus2pt} }}% {\end{list}} \renewcommand\th{${}^\text{th}$} \begin{document} \gdef\Partage{0.7} \setlength{\parindent}{0cm} \begin{titlepage} \null\par\vfill \begin{center} \begin{minipage}{0.75\linewidth} \begin{center} \Huge\bfseries xstring\par\vspace{5pt} \small v1.0\par\vspace{25pt} \normalsize User's manual \end{center} \end{minipage} \end{center} \vspace{1cm} \begin{center} Christian {\sc Tellechea}\par\small \href{mailto:unbonpetit@gmail.com}{\nolinkurl{unbonpetit@gmail.com}}\par\vspace{5pt} July 6\th{} 2008 \end{center} \vfill\hrulefill \begin{center} \begin{minipage}{0.85\linewidth} \noindent \hfill\textbf{\textit{Abstract}}\hfill{}\medskip\par This package groups together macros manipulating strings, such as: \setlength{\parindent}{1.5em} \begin{itemize} \item[$\triangleright$] tests: \begin{itemize} \item does a string contains at least $n$ times an another? \item does a string starts (or ends) with another? etc. \end{itemize} \item[$\triangleright$] extractions of substrings: \begin{itemize} \item what is on the left (or the right) of the $n$\th{} occurrence of a substring; \item what is between the occurrences of 2 substrings; \item substring between 2 positions, etc. \end{itemize} \item[$\triangleright$] substitution of all, or the $n$ first occurrences of a substring for an other substring; \item[$\triangleright$] calculation of numbers: \begin{itemize} \item length of a string; \item position of the $n$\th{} occurrence of a substring; \item how many times a string contains a substring? \end{itemize} \end{itemize} \medskip For programming purposes, other macros allow to use special characters (\verb|&|, \verb|~|, \verb|\|, \verb|{|, \verb|}|, \verb|_|, \verb|#|, \verb|$|, \verb|^| and \verb|%|) with the macros manipulating strings. \end{minipage} \end{center} \hrulefill\vfill{} \end{titlepage} \pagebreak \tableofcontents \pagebreak This manual is a translation of the french manual. I apologize for my poor english but I did my best, and I hope that the following is comprehensible! \section{Presentation} \subsection{Description} This extension provides macros and tests operating on strings, as other programmation languages have. They provides the usual strings operations, such as: test if a string contains another, begins or ends with another, extractions of strings, calculation of the position of a substring, of the number of occurrences, etc.\medskip Certainly, other packages exit (for example \href{http://www.ctan.org/tex-archive/macros/latex/contrib/substr/}{\nolinkurl{substr}} and \href{http://www.ctan.org/tex-archive/macros/latex/contrib/stringstrings/}{\nolinkurl{stringstrings}}), but as well as differences on features, they do not take into account occurrences so I found them too limited and difficult to use for programming. \subsection{Motivation} I decided to write this package of macros because I have never really found tools in \LaTeX{} suiting my needs for strings. So, over the last few months,I wrote a few macros that I occasionally or regularly used. Their numbers have increased and become a little too dispersed in directories in my computer, so I have grouped them together in this package. Thus, writing a coherent set of macros forces more discipline and leads to necessary improvements, which took most of the time I spent writing this package.\medskip This package is my first one as I discoverd \LaTeX{} less than a year ago, so my main motivation was to make progress in programming with \TeX, and to tackle its specific methods. \subsection{Operation} \label{deftexte} In the following, \guill{\texte{10,11,12}} means a string made of characters whose catcodes are 10, 11 or 12. \subsubsection{Expansion of arguments} \label{devarg} All the arguments of the macros operating on strings\footnote{Excepted the 2 last arguments of the tests.} are supposed, after a number of times of expansion, to expand to \texte{10,11,12}. By \emph{default}, to avoid many \verb|\expandafter| and to ease the use of macros, all the arguments are fully expanded before being taken into account by the macro: for this, \verb|\fullexpandarg| is called by default.\bigskip For example, if \verb|\macro| is a macro of this package requiring 2 arguments (text for the first and a number for the second), the following structures are equivalent:\medskip \begin{minipage}[t]{0.35\linewidth} Structure with \verb|\fullexpandarg|\par\hrulefill\par \verb|\def\aa{some text}|\par \verb|\def\nn{2}|\par \verb|\macro{\aa}{\nn}| \end{minipage} \hfill \begin{minipage}[t]{0.6\linewidth} Usual structure with \LaTeX{} or with \verb|\normalexpandarg|\par\hrulefill\par \verb|\def\aa{some text}|\par \verb|\def\nn{2}|\par \verb|\expandafter\expandafter\expandafter\macro|\par \verb| \expandafter\expandafter\expandafter|\par \verb| {\expandafter\aa\expandafter}\expandafter{\nn}| \end{minipage}\hfill{}\medskip The structure on the left allow to forget the order of expansion and avoid writing many \verb|\expandafter|. On the other hand, the arguments must be purely expandable into \texte{10,11,12} containing what is expected by the macro (number or string).\medskip However, at any time, you can find the usual order of expansion with the macro \verb|\normalexpandarg|, and use again \verb|\fullexpandarg| if you want a full expansion of the arguments. \subsubsection{Textual arguments} The macros operating on strings require one or several arguments containing --~or whose expansion contains~-- \texte{10,11,12}\voirdeftexte{}, using the usual syntax \verb|{|\texte{10,11,12}\verb|}|, and for optionnal arguments \verb|[|\texte{10,11,12}\verb|]|.\medskip The following rules shoud be observed for the expansion of textual arguments: \begin{itemize} \item they can contain letters (uppercase or lowercase, accented\footnote{For a reliable operation with accented letters, the \texttt{\textbackslash fontenc} package with option \texttt{[T1]} and \texttt{\textbackslash inputenc} with appropriated option must be loaded} or not), figures, spaces, and any other character with a catcode of 10, 11 ou 12 (punctuation signs, calculation signs, parenthesis, square bracket, etc). On the other hand, the \officialeuro{} sign is not allowed. \item spaces are taken into account as normal characters, except if several spaces follows in which case the \LaTeX{} rule prevails and they become a single space; \item no special character is allowed, i.e. the 10 following characters are strictly forbiden: \verb|&|, \verb|~|, \verb|\|, \verb|{|, \verb|}|, \verb|_|, \verb|#|, \verb|$|, \verb|^| and \verb|%|. \end{itemize}\medskip To circumvent some of these rules and to go further in the use of the macros operating on strings, this package provides special macros that enable special characters in textual arguments. See the detailed description of this modus operandi in chapter~\ref{programmation}, page~\pageref{programmation}. \subsubsection{Expansion of macros, optional argument} The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \verb|\edef|. Consequently, some structures are not allowed and lead to errors when compiling. If, for example, \verb|\command{argument}| is a macro of this package operating on strings and returning a string, the following structures are not allowed:\smallskip \hspace{0.2\linewidth}\verb|\edef\Result{\command{argument}}|\par \qquad or this nested structure\par \hspace{0.2\linewidth}\verb|\commandA{\commandB{\commandC{argument}}}|\smallskip For this reason, all the macros returning a result (i.e. all excepted the tests) have an optionnal argument in last position. The syntax is \arguC{nom}, where \argu{nom} is the name of the control sequence that will receive the result of the macro: the assignment is made with an \verb|\edef| which make the result of the macro |\argu{nom} purely expandable. Of course, if an optionnal argument is present, the macro does not display anything.\medskip Thus, this structure not allowed, already seen above:\par \hspace{0.2\linewidth}\verb|\edef\Resultat{\commande{arguments}}|\par \qquad is equivalent to:\par \hspace{0.2\linewidth}\verb|\commande{argument}[\Resultat]|\medskip And this nested one:\par \hspace{0.2\linewidth}\verb|\commandeA{\commandeB{\commandeC{arguments}}}|\par \qquad can be replaced by:\par \hspace{0.2\linewidth}\verb|\commandeC{arguments}[\MaChaine]|\par \hspace{0.2\linewidth}\verb|\commandeB{\MaChaine}[\MaChaine]|\par \hspace{0.2\linewidth}\verb|\commandeA{\MaChaine}| \section{The macros} \label{listemacros} \subsection{Presentation of macros} In the following chapters, all the macros will be presented this plan:\smallskip \begin{itemize} \item the syntax and the value of optional arguments \item a short description of the operation; \item the operation under special conditions. For each conditions considered, the operation described has priority on that (those) below; \item finally, several examples are given. I tried to find them most easily comprehensible and most representative of the situations met in normal use\footnote{For more examples, see the test file.}. If a doubt is possible with spaces in the result, this one will be delimited by \guill{|}, given that an empty string is represented by \guill{||}. \end{itemize} \subsection{The tests} \subsubsection{IfSubStr} \verb|\IfSubStr|\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{true}\ARGU{false} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip Tests if \argu{string} contains at least \argu{number} times \argu{stringA} and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{number}${}\leqslant0$, runs \argu{false}; \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \Exemple|\IfSubStr{xstring}{tri}{true}{false}| \Exemple|\IfSubStr{xstring}{a}{true}{false}| \Exemple|\IfSubStr{a bc def }{c d}{true}{false}| \Exemple|\IfSubStr{a bc def }{cd}{true}{false}| \Exemple|\IfSubStr[2]{1a2a3a}{a}{true}{false}| \Exemple|\IfSubStr[3]{1a2a3a}{a}{true}{false}| \Exemple|\IfSubStr[4]{1a2a3a}{a}{true}{false}| \subsubsection{IfSubStrBefore} \verb|\IfSubStrBefore|\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, tests if the \argu{number1}\th{} occurrence of \argu{stringA} is on the left of the \argu{number2}\th{} occurrence of \argu{stringB}. Runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If one of the occurrences is not found, it runs \argu{false}; \item If one of the arguments \argu{string}, \argu{stringA} or \argu{stringB} is empty, runs \argu{false}; \item If one of the optional arguments is negative or zero, runs \argu{false}. \end{Conditions} \Exemple|\IfSubStrBefore{xstring}{st}{in}{true}{false}| \Exemple|\IfSubStrBefore{xstring}{ri}{s}{true}{false}| \Exemple|\IfSubStrBefore{LaTeX}{LaT}{TeX}{true}{false}| \Exemple|\IfSubStrBefore{a bc def }{ b}{ef}{true}{false}| \Exemple|\IfSubStrBefore{a bc def }{ab}{ef}{true}{false}| \Exemple|\IfSubStrBefore[2,1]{b1b2b3}{b}{2}{true}{false}| \Exemple|\IfSubStrBefore[3,1]{b1b2b3}{b}{2}{true}{false}| \Exemple|\IfSubStrBefore[2,2]{baobab}{a}{b}{true}{false}| \Exemple|\IfSubStrBefore[2,3]{baobab}{a}{b}{true}{false}| \subsubsection{IfSubStrBehind} \verb|\IfSubStrBehind|\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, tests if the \argu{number1}\th{} occurrence of \argu{stringA} is on the right of the \argu{number2}\th{} occurrence of \argu{stringB}. Runs \argu{true} if so, ands \argu{false} otherwise.\medskip \begin{Conditions} \item If one of the occurrences is not found, it runs \argu{false}; \item If one of the arguments \argu{string}, \argu{stringA} or \argu{stringB} is empty, runs \argu{false}; \item If one of the optional arguments is negative or zero, runs \argu{false}. \end{Conditions} \Exemple|\IfSubStrBehind{xstring}{ri}{xs}{true}{false}| \Exemple|\IfSubStrBehind{xstring}{s}{i}{true}{false}| \Exemple|\IfSubStrBehind{LaTeX}{TeX}{LaT}{true}{false}| \Exemple|\IfSubStrBehind{a bc def }{ d}{a}{true}{false}| \Exemple|\IfSubStrBehind{a bc def }{cd}{a b}{true}{false}| \Exemple|\IfSubStrBehind[2,1]{b1b2b3}{b}{2}{true}{false}| \Exemple|\IfSubStrBehind[3,1]{b1b2b3}{b}{2}{true}{false}| \Exemple|\IfSubStrBehind[2,2]{baobab}{b}{a}{true}{false}| \Exemple|\IfSubStrBehind[2,3]{baobab}{b}{a}{true}{false}| \subsubsection{IfBeginWith} \verb|\IfBeginWith|\ARGU{string}\ARGU{stringA}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{string} begins with \argu{stringA}, and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \Exemple|\IfBeginWith{xstring}{xst}{true}{false}| \Exemple|\IfBeginWith{LaTeX}{a}{true}{false}| \Exemple|\IfBeginWith{a bc def }{a b}{true}{false}| \Exemple|\IfBeginWith{a bc def }{ab}{true}{false}| \subsubsection{IfEndWith} \verb|\IfEndWith|\ARGU{string}\ARGU{stringA}\ARGU{Behind}\ARGU{false} \smallskip Tests if \argu{string} ends with \argu{stringA}, and runs \argu{true} if so, and \argu{false} otherwise.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, runs \argu{false}. \end{Conditions} \Exemple|\IfEndWith{xstring}{ring}{true}{false}| \Exemple|\IfEndWith{LaTeX}{a}{true}{false}| \Exemple|\IfEndWith{a bc def }{ef }{true}{false}| \Exemple|\IfEndWith{a bc def }{ef}{true}{false}| \subsection{Extraction of substrings} \subsubsection{StrBefore} \verb|\StrBefore|\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns what is leftwards the \argu{number}\th{} occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, an empty string is returned; \item If \argu{number}${}<1$ then the macro behaves as if \argu{number}${}=1$; \item If the occurrence is not found, an empty string is returned. \end{Conditions} \Exemple*|\StrBefore{xstring}{tri}| \Exemple*|\StrBefore{LaTeX}{e}| \Exemple*|\StrBefore{LaTeX}{p}| \Exemple*|\StrBefore{LaTeX}{L}| \Exemple*|\StrBefore{a bc def }{def}| \Exemple*|\StrBefore{a bc def }{cd}| \Exemple*|\StrBefore[1]{1b2b3}{b}| \Exemple*|\StrBefore[2]{1b2b3}{b}| \subsubsection{StrBehind} \verb|\StrBehind|\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns what is rightwards the \argu{number}\th{} occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{string} or \argu{stringA} is empty, an empty string is returned; \item If \argu{number}${}<1$ then the macro behaves as if \argu{number}${}=1$; \item If the occurrence is not found, an empty string is returned. \end{Conditions} \Exemple*|\StrBehind{xstring}{tri}| \Exemple*|\StrBehind{LaTeX}{e}| \Exemple*|\StrBehind{LaTeX}{p}| \Exemple*|\StrBehind{LaTeX}{X}| \Exemple*|\StrBehind{a bc def }{bc}| \Exemple*|\StrBehind{a bc def }{cd}| \Exemple*|\StrBehind[1]{1b2b3}{b}| \Exemple*|\StrBehind[2]{1b2b3}{b}| \Exemple*|\StrBehind[3]{1b2b3}{b}| \subsubsection{StrBetween} \verb|\StrBetween|\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip The values of the optional arguments \argu{number1} and \argu{number2} are 1 by default.\par\smallskip In \argu{string}, returns the substring between\footnote{In a strict sense, i.e. \emph{without} the strings \argu{stringA} and \argu{stringB}} the \argu{number1}\th{} occurrence of \argu{stringA} and \argu{number2}\th{} occurrence of \argu{stringB}.\medskip \begin{Conditions} \item If the occurrences are not in this order ---~\argu{stringA} \emph{followed by} \argu{stringB}~--- in \argu{string}, an empty string is returned; \item If one of the 2 occurrences doesn't exist in \argu{string}, an empty string is returned; \item If one of the optional arguments \argu{number1} ou \argu{number2} is negative or zero, an empty string is returned. \end{Conditions} \Exemple*|\StrBetween{xstring}{xs}{ng}| \Exemple*|\StrBetween{xstring}{i}{n}| \Exemple*|\StrBetween{xstring}{a}{tring}| \Exemple*|\StrBetween{a bc def }{a}{d}| \Exemple*|\StrBetween{a bc def }{a }{f}| \Exemple*|\StrBetween{a1b1a2b2a3b3}{a}{b}| \Exemple*|\StrBetween[2,3]{a1b1a2b2a3b3}{a}{b}| \Exemple*|\StrBetween[1,3]{a1b1a2b2a3b3}{a}{b}| \Exemple*|\StrBetween[3,1]{a1b1a2b2a3b3}{a}{b}| \Exemple*|\StrBetween[3,2]{abracadabra}{a}{bra}| \subsubsection{StrSubstitute} \verb|\StrSubstitute|\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, substitute the \argu{number} first occurrences of \argu{stringA} for \argu{stringB}, except if \argu{number}${}=0$ in which case \emph{all} the occurrences are substituted. \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{stringA} is empty or doesn't exist in \argu{string}, the macro is ineffective; \item If \argu{number} is greater than the number of occurrences of \argu{stringA}, then all the occurrences are substituted; \item If \argu{number}${}<0$ the macro behaves as if \argu{number}${}=0$; \item If \argu{stringB} is empty, the occurrences of \argu{stringA}, if they exist, are deleted. \end{Conditions} \Exemple|\StrSubstitute{xstring}{i}{a}| \Exemple|\StrSubstitute{abracadabra}{a}{o}| \Exemple|\StrSubstitute{abracadabra}{br}{TeX}| \Exemple|\StrSubstitute{LaTeX}{m}{n}| \Exemple|\StrSubstitute{a bc def }{ }{M}| \Exemple|\StrSubstitute{a bc def }{ab}{AB}| \Exemple|\StrSubstitute[1]{a1a2a3}{a}{B}| \Exemple|\StrSubstitute[2]{a1a2a3}{a}{B}| \Exemple|\StrSubstitute[3]{a1a2a3}{a}{B}| \Exemple|\StrSubstitute[4]{a1a2a3}{a}{B}| \subsubsection{StrDel} \verb|\StrDel|\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip Delete the \argu{number} first occurrences of \argu{stringA} in \argu{string}, except if \argu{number}${}=0$ in which case \emph{all} the occurrences are deleted.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{stringA} is empty or doesn't exist in \argu{string}, the macro is ineffective; \item If \argu{number} greater then the number of occurrences of \argu{stringA}, then all the occurrences are deleted; \item If \argu{number}${}<0$ the macro behaves as if \argu{number}${}=0$; \end{Conditions} \Exemple|\StrDel{abracadabra}{a}| \Exemple|\StrDel[1]{abracadabra}{a}| \Exemple|\StrDel[4]{abracadabra}{a}| \Exemple|\StrDel[9]{abracadabra}{a}| \Exemple|\StrDel{a bc def }{ }| \subsubsection{StrGobbleLeft} \verb|\StrGobbleLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} fisrt characters on the left.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{number}${}\leqslant0$, no character is deleted; \item If \argu{number}${}\geqslant{}$\argu{lengthString}, all the characters are deleted. \end{Conditions} \Exemple*|\StrGobbleLeft{xstring}{2}| \Exemple*|\StrGobbleLeft{xstring}{9}| \Exemple*|\StrGobbleLeft{LaTeX}{4}| \Exemple*|\StrGobbleLeft{LaTeX}{-2}| \Exemple*|\StrGobbleLeft{a bc def }{4}| \subsubsection{StrLeft} \verb|\StrLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} fisrt characters on the left.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{number}${}\leqslant0$, no character is returned; \item If \argu{number}${}\geqslant{}$\argu{lengthString}, all the characters are returned. \end{Conditions} \Exemple*|\StrLeft{xstring}{2}| \Exemple*|\StrLeft{xstring}{9}| \Exemple*|\StrLeft{LaTeX}{4}| \Exemple*|\StrLeft{LaTeX}{-2}| \Exemple*|\StrLeft{a bc def }{5}| \subsubsection{StrGobbleRight} \verb|\StrGobbleRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} last characters on the right.\medskip \Exemple*|\StrGobbleRight{xstring}{2}| \Exemple*|\StrGobbleRight{xstring}{9}| \Exemple*|\StrGobbleRight{LaTeX}{4}| \Exemple*|\StrGobbleRight{LaTeX}{-2}| \Exemple*|\StrGobbleRight{a bc def }{4}| \subsubsection{StrRight} \verb|\StrRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} last characters on the right.\medskip \Exemple*|\StrRight{xstring}{2}| \Exemple*|\StrRight{xstring}{9}| \Exemple*|\StrRight{LaTeX}{4}| \Exemple*|\StrRight{LaTeX}{-2}| \Exemple*|\StrRight{a bc def }{5}| \subsubsection{StrChar} \verb|\StrChar|\ARGU{string}\ARGU{number}\arguC{name} \smallskip Returns the character at the position \argu{number} in \argu{string}.\medskip \begin{Conditions} \item If \argu{string} is empty, no caracter is returned; \item If \argu{number}${}\leqslant0$ or if \argu{number}${}>{}$\argu{lengthString}, no character is returned. \end{Conditions} \Exemple|\StrChar{xstring}{4}| \Exemple*|\StrChar{xstring}{9}| \Exemple*|\StrChar{xstring}{-5}| \Exemple|\StrChar{a bc def }{6}| \subsubsection{StrMid} \verb|\StrMid|\ARGU{string}\ARGU{numberA}\ARGU{numberB}\arguC{name} \smallskip In \argu{string}, returns the substring between\footnote{In the broad sense, i.e. that the strings characters of the \guill{border} are returned.} the positions \argu{numberA} and \argu{numberB}.\medskip \begin{Conditions} \item If \argu{string} is empty, an empty string is returned; \item If \argu{numberA}${}>{}$\argu{numberB}, an empty string is returned; \item If \argu{numberA}${}<1$ and \argu{numberB}${}<1$ an empty string is returned; \item If \argu{numberA}${}>{}$\argu{lengthString} et \argu{numberB}${}>{}$\argu{lengthString}, an empty string is returned; \item If \argu{numberA}${}<1$, the macro behaves as if \argu{numberA}${}=1$; \item If \argu{numberB}${}>{}$\argu{lengthString}, the macro behaves as if \argu{numberB}${}={}$\argu{lengthString}. \end{Conditions} \Exemple|\StrMid{xstring}{2}{5}| \Exemple|\StrMid{xstring}{-4}{2}| \Exemple*|\StrMid{xstring}{5}{1}| \Exemple|\StrMid{xstring}{6}{15}| \Exemple|\StrMid{xstring}{3}{3}| \Exemple*|\StrMid{a bc def }{2}{7}| \subsection{Number results} \subsubsection{StrLen} \verb|\StrLen|\ARGU{string}\arguC{name} \smallskip Return the length of \argu{string}. \Exemple|\StrLen{xstring}| \Exemple|\StrLen{A}| \Exemple|\StrLen{a bc def }| \subsubsection{StrCount} \verb|\StrCount|\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip Counts how many times \argu{stringA} is contained in \argu{string}.\par\medskip \begin{Conditions} \item If one at least of the arguments \argu{string} or \argu{stringA} is empty, the macro return 0. \end{Conditions} \Exemple|\StrCount{abracadabra}{a}| \Exemple|\StrCount{abracadabra}{bra}| \Exemple|\StrCount{abracadabra}{tic}| \Exemple|\StrCount{aaaaaa}{aa}| \subsubsection{StrPosition} \verb|\StrPosition|\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip The value of the optional argument \argu{number} is 1 by default.\par\smallskip In \argu{string}, returns the position of the \argu{number}\th{} occurrence of \argu{stringA}.\medskip \begin{Conditions} \item If \argu{number} is greater than the number of occurrences of \argu{stringA}, then the macro returns 0; \item If \argu{string} doesn't contain \argu{stringA}, then the macro returns 0. \end{Conditions} \medskip \Exemple|\StrPosition{xstring}{ring}| \Exemple|\StrPosition[4]{abracadabra}{a}| \Exemple|\StrPosition[2]{abracadabra}{bra}| \Exemple|\StrPosition[9]{abracadabra}{a}| \Exemple|\StrPosition{abracadabra}{z}| \Exemple|\StrPosition{a bc def }{d}| \Exemple|\StrPosition[3]{aaaaaa}{aa}| \section{Using the macros for programming purposes} \label{programmation} \subsection{Verbatimize to a control sequence} The macro \verb|\verbtocs| allow to read the content of a \guill{verb} argument containing special characters: \verb|&|, \verb|~|, \verb|\|, \verb|{|, \verb|}|, \verb|_|, \verb|#|, \verb|$|, \verb|^| et \verb|%|. The catcodes of \guill{normal} characters are left unchanged while special characters take a catcode 12. Then, these characters are assigned to a control sequence. The syntax is:\medskip \hfill\verb|\verbtocs|\ARGU{name}|\argu{characters}|\hfill{} \smallskip \argu{name} is the name of the control sequence receiving, with an \verb|\edef|, the \argu{characters}. Consequently, \argu{name} contains \texte{10,11,12}\voirdeftexte.\medskip By default, the character delimiting the verb content is \guill{|}. Obviously, this character cannot be both delimiting and being contained into what it delimits. If you need to verbatimize characters containing \guill{|}, you can change at any time the character delimiting the verb content with the macro:\par\medskip \hfill\verb|\setverbdelim|\ARGU{character}\hfill{}\smallskip Any \argu{character} with a catcode 11 or 12 can be used\footnote{Several characters can be used, but the syntax of \texttt{\textbackslash verbtocs} becomes less readable ! For this reason, a warning occurs when the argument of \texttt{\textbackslash setverbdelim} contains more than a single character.}. For example, after \verb|\setverbdelim{=}|, a verb argument look like this: \verb|=|\argu{characters}\verb|=|.\medskip About verb arguments, keep in mind that: \begin{itemize} \item all the characters before |\argu{characters}| are ignored; \item inside the verb argument, all the spaces are taken into account, even if they are consecutive. \end{itemize} \medskip Example:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\result} |a & b{ c% d$ e \f| \result \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \verbtocs{\result} |a & b{ c% d$ e \f| \result \end{minipage} \subsection{Tokenization of a text to a control sequence} The reverse process of what has been seen above is to transform a \texte{10,11,12} into control sequences. This is done by the macro:\medskip \hfill\verb|\tokenize|\ARGU{name}\ARGU{control sequence}\hfill{} \smallskip \argu{control sequence} is fully expanded if \verb|\fullexpandarg| has been called (see page~\pageref{devarg}), and expanded 1 time if \verb|\normalexpandarg| has benn called. In both cases, the expansion must be \texte{10,11,12}. Then, this \texte{10,11,12} is converted into tokens and assigned with a \verb|\def| to the control sequence \argu{name}.\medskip Example:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\text}|\textbf{a} $\frac{1}{2}$| text: \text \tokenize{\result}{\text} \par result: \result \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \raggedright \verbtocs{\text} |\textbf{a} $\frac{1}{2}$| text: \text \tokenize{\result}{\text} \par result: \result \end{minipage} \medskip Obviously, the control sequence \verb|\result| can be called at the last line since the control sequences it contains are defined. \subsection{Expansion of a control sequence before verbatimize} \subsubsection{The scancs macro} It is possible to expand $n$ times a control sequence before converting this expansion into text. This is done by the macro:\medskip \hfill\verb|\scancs|\arguC{number}\ARGU{name}\ARGU{control sequence}\hfill{} \smallskip \argu{number}${}=1$ by default and represents the number of times \argu{control sequence} will be expanded before being converted in characters with catcodes 12 (or 10 for spaces). These characters are then assigned to \argu{name}. \subsubsection{Mind the catcodes !} Let's take a simple example where \argu{control sequence} expands to text:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\test{a b1 d} \scancs{\result}{\test} \resultat \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\test{a b1 d} \scancs{\result}{\test} \result \end{minipage} \medskip But mind the catcodes ! In this example, \verb|\scancs{\result}{\test}| is not equivalent to \verb|\edef\result{\test}|.\medskip Indeed, with \verb|\scancs{\resultat}{\test}|, \verb|\result| contains \texte{10,12} and expands to:\par \hfill\verb|a|${}_{12}$\ \verb*| |${}_{10}$\ \verb|b|${}_{12}$\ \verb|1|${}_{12}$\ \verb*| |${}_{10}$\ \verb|d|${}_{12}$\hfill{} \medskip With \verb|\edef\resultat{\test}|, \verb|\resultat| contains \texte{10,11,12}, i.e. characters whose catcodes are 11 (the letters), 12 (the figure 1) and 10 (the spaces). It expands to:\par \hfill\verb|a|${}_{11}$\ \verb*| |${}_{10}$\ \verb|b|${}_{11}$\ \verb|1|${}_{12}$\ \verb*| |${}_{10}$\ \verb|d|${}_{11}$\hfill{} \subsubsection{Several expansions} If necessary, the number of expansions can be controled with the optional argument. In the following example, when \verb|\scancs| is called the first time, \verb|\c| is expanded 3 times and gives "\verb|1|${}_{12}$\ \verb*| |${}_{10}$\ \verb|z|${}_{11}$\ \verb*| |${}_{10}$\ \verb|3|${}_{12}$" which is converted into "\verb|1|${}_{12}$\ \verb*| |${}_{10}$\ \verb|z|${}_{12}$\ \verb*| |${}_{10}$\ \verb|3|${}_{12}$".\smallskip On the other hand, if after $n$ expansions, the result is a control sequence, this control sequence is transformed into characters with catcodes 12. In the example above, when \verb|\scancs| is called the second time, \verb|\scancs[2]{\resultat}{\c}| expands \verb|\c| 2 times: this gives the control sequence \fbox{\texttt{\textbackslash a}} which is converted into "\verb|\|${}_{12}$~\verb|a|${}_{12}$".\medskip This example show all the "depths" of expansion, from 3 to 0:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\a{1 z 3} \def\b{\a} \def\c{\b} \scancs[3]{\result}{\c} \result\par \scancs[2]{\result}{\c} \result\par \scancs[1]{\result}{\c} \result\par \scancs[0]{\result}{\c} \result \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\a{1 z 3} \def\b{\a} \def\c{\b} \scancs[3]{\result}{\c} \result\par \scancs[2]{\result}{\c} \result\par \scancs[1]{\result}{\c} \result\par \scancs[0]{\result}{\c} \result \end{minipage} \medskip Obviously, it is necessary to ensure that the expansion to the desired depth is possible. \subsubsection{Expansion of several control sequences} In normal use, the third argument \argu{control sequence} (or one of its expansions) must contain a single control sequence that will be expanded. If this third argument or one of its expansion contains several control sequences, compilation stops with an error message asking you to use the starred version. This starred version, more difficult to use allows to expand \argu{number} times \emph{all} the control sequences contained in the third argument. Let's see this with this example:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\a{LaTeX} \def\b{is powerful} \scancs*[1]{\result}{\a \b} \result\par \scancs*[2]{\result}{\a\space\b} \result \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\a{LaTeX} \def\b{is powerful} \scancs*[1]{\result}{\a \b} \result\par \scancs*[2]{\result}{\a\space\b} \result \end{minipage} \medskip First of all, a warning message has been sent to log: "if third argument or its expansion have braces or spaces, they will be removed when scanned! Use starred \verb|\scancs*| macro with care". Let's see what it means\ldots\medskip In the first result, a space is missing between the words \guill{LaTeX} and \guill{is}, though a space was present in the code between the 2 control sequences \verb|\a| and \verb|\b|. Indeed, \TeX{} ignores spaces that follow control sequences. Consequently, \verb|{\a \b}| is read as \verb|{\a\b}|, whatever be the number of spaces in the code between \verb|\a| and \verb|\b|. To obtain a space between \guill{LaTeX} and \guill{is}, we could have used the control sequence \verb|\space| whose expansion is a space, and write for the third argument: \verb|{\a\space\b}|. We could also have modified the defintion of \verb|\a| with a space after the word "LaTeX" like this: \verb|\def\a{LaTeX }|.\medskip However, it is necessary to be carfull when expanding control sequences more than one time: if a control sequence is expanded $n$ times and gives \texte{10,11,12}, the next expansion gobbles spaces. The second result shows that the second expansion gobbled all the spaces and consequently, \verb|\result| contains \guill{LaTeXispowerful}!\medskip Moreover, it's also the meaning of the warning message, if the $n$\th{} expansion of a control sequence contains braces, they will be gobbled, like spaces.\medskip Finaly, when using \verb|\scancs| a space is inserted after each control sequence. Indeed, \verb|\detokenize| (an $\varepsilon$-\TeX{} command) called by \verb|\scancs| inserts a space after each control sequence. There is no way to avoid this. \subsubsection{Examples} In the following example, control sequences are expanded 2 times: \verb|\d| gives \verb|\b|, and \verb|\b| gives\\\verb|\textbf{a}\textit{b}|. Notice that a space is inserted after each control sequence.\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\a{\textbf{a}\textit{b}} \def\b{\a} \def\c{\b} \def\d{\c} \scancs*[2]{\result}{\d\b} \result \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\a{\textbf{a}\textit{b}} \def\b{\a} \def\c{\b} \def\d{\c} \scancs*[2]{\result}{\d\b} \result \end{minipage} \medskip This is an example that shows the deletion of braces during the next expansion:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\a{1{2}} \def\b{\a} \scancs*[1]{\result}{\b{A}} \result\par \scancs*[2]{\result}{\b{A}} \result\par \scancs*[3]{\result}{\b{A}} \result\par \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\a{1{2}} \def\b{\a} \scancs*[1]{\result}{\b{A}} \result\par \scancs*[2]{\result}{\b{A}} \result\par \scancs*[3]{\result}{\b{A}} \result\par \end{minipage} \medskip Finaly, here is an example where we take advantage of the space inserted after each sequence control to find the $n$\th{} control sequence in the expansion of a control sequence.\medskip In the example above, we find the fourth control sequence in \verb|\myCS| whose expansion is:\par\medskip \hfill\verb|\a xy{3 2}\b7\c123 {m}\d{8}\e|\hfill{} \medskip Obviously, we expect: \verb|\d|\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\antislash}|\| \newcommand\findcs[2]{% \scancs[1]{\theCS}{#2}% \tokenize{\theCS}{\theCS}% \scancs[1]{\theCS}{\theCS}% \StrBehind[#1]{\theCS}{\antislash}[\theCS]% \StrBefore{\theCS}{ }[\theCS]% \edef\theCS{\antislash\theCS}} \verbtocs{\myCS}|\a xy{3 2}\b7\c123 {m}\d{8}\e| % here, \myCS contains text \findcs{4}{\myCS} \theCS\par \def\myCS{\a xy{3 2}\b7\c123 {m}\d{8}\e} % here, \myCS contains control sequences \findcs{4}{\myCS} \theCS \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \verbtocs{\antislash}|\| \newcommand\findcs[2]{% \scancs[1]{\theCS}{#2}% \tokenize{\theCS}{\theCS}% \scancs[1]{\theCS}{\theCS}% \StrBehind[#1]{\theCS}{\antislash}[\theCS]% \StrBefore{\theCS}{ }[\theCS]% \edef\theCS{\antislash\theCS}} \verbtocs{\myCS}|\a xy{3 2}\b7\c123 {m}\d{8}\e| % here, \myCS contains text \findcs{4}{\myCS} \theCS\par \def\myCS{\a xy{3 2}\b7\c123 {m}\d{8}\e} % here, \myCS contains control sequences \findcs{4}{\myCS} \theCS \end{minipage} \subsection{Inside the definition of a macro} Some difficulties arise inside the definition of a macro, i.e. between braces following a \verb|\def\macro| or a\\\verb|\newcommand\macro|.\medskip It is forbidden to use the command \verb|\verb| inside the definition of a macro. For the same reasons:\par\medskip \hfill\textbf{Do not use \texttt{\textbackslash verbtocs} inside the definition of a macro}.\hfill{}\medskip But then, how to manipulate special characters and "verbatimize" inside the définition of macros ?\bigskip The \verb|\detokenize| primitive of $\varepsilon$-\TeX can be used but it has limitations: \begin{itemize} \item braces must be balanced; \item consecutive spaces make a single space; \item the \verb|%| sign is not allowed; \item a space is inserted after each control sequence; \item \verb|#| signs become \verb|##|. \end{itemize} \medskip It is better to use \verb|\scancs| and define \emph{outside the definition of the macros} control sequences containing special characters with \verb|\verbtocs|. It is also possible to use \verb|\tokenize| to transform the final result (which is generaly \texte{10,11,12}) into control sequences. See example using these macros at the end of this manual, page~\pageref{exemples}.\medskip In the following teaching example\footnote{It is possible to make much more simple using \texttt{\textbackslash detokenize}. The macro becomes:\par\texttt{\textbackslash newcommand\textbackslash bracearg[1]\{\textbackslash detokenize\{\{\#1\}\}\}}}, the macro \verb|\bracearg| adds braces to its argument. To make this possible, 2 control sequences \verb|\Ob| and \verb|\Cb| containing "\verb|{|" and "\verb|}|" are defined outside the definition of \verb|\bracearg|, and expanded inside it:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\Ob}|{| \verbtocs{\Cb}|}| \newcommand\bracearg[1]{% \def\text{#1}% \scancs*{\result}{\Ob\text\Cb}% \result} \bracearg{xstring}\par \bracearg{\a} \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \verbtocs{\Ob}|{| \verbtocs{\Cb}|}| \newcommand\bracearg[1]{% \def\text{#1}% \scancs*{\result}{\Ob\text\Cb}% \result} \bracearg{xstring}\par \bracearg{\a} \end{minipage} \subsection{Starred macros} As \verb|\scancs| returns \texte{10,12}\voirdeftexte, some unexpected results occur with the macros seen at chapter~\ref{listemacros} because they care the catcodes of the characters of their arguments.\medskip This is an example of such malfunctioning:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\mytext}|a b c| \IfSubStr{\mytext}{b}{true}{false} \par \edef\onecs{x y z} \scancs[1]\mycs\onecs \IfSubStr{\mycs}{y}{true}{false} \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \verbtocs{\mytext}|a b c| \IfSubStr{\mytext}{b}{true}{false} \par \edef\onecs{x y z} \scancs[1]\mycs\onecs \IfSubStr{\mycs}{y}{true}{false} \end{minipage} \medskip The first test is "true" since catcodes of non special characters are left unchanged by \verb|\verbtocs|: indeed, \verb|\mytext| contains "\verb|a|${}_{11}$\ \verb*| |${}_{10}$\ \verb|b|${}_{11}$\ \verb*| |${}_{10}$\ \verb|c|${}_{11}$" which does contain the second argument "\verb|b|${}_{11}$".\medskip With the second test, since \verb|\scancs| returns \texte{10,12}, it is false. \verb|\mycs| contains "\verb|x|${}_{12}$\ \verb*| |${}_{10}$\ \verb|y|${}_{12}$\ \verb*| |${}_{10}$\ \verb|z|${}_{12}$" which does not contains the second argument ""\verb|y|${}_{11}$".\bigskip To avoid this annoyance due unmatching catcodes, it is possible to make macros of chapter~\ref{listemacros} compatible with \verb|\scancs|: they all have a starred version that converts textual arguments into \texte{10,12}, i.e. characters whose catcodes are 10 ou 12:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \edef\onecs{x y z} \scancs[1]\mycs\onecs \IfSubStr*{\mycs}{y}{true}{false} \end{boxedverbatim} \hspace{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \edef\onecs{x y z} \scancs[1]\mycs\onecs \IfSubStr*{\mycs}{y}{true}{false} \end{minipage} \subsection{Examples} \label{exemples} Here are some very simple examples involving the macros of this package in programming purposes. \subsubsection{Example 1} We want to substitute the 2 first \verb|\textit| by \verb|\textbf| in the control sequence \verb|\myCS| winch contains \par\smallskip \hfill\verb|\textit{A}\textit{B}\textit{C}|\hfill{} \medskip We expect: \textbf{A}\textbf{B}\textit{C}\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\myCS{\textit{A}\textit{B}\textit{C}} \scancs[1]{\text}{\myCS} \StrSubstitute*[2]{\text}{textit}{textbf}[\text] \tokenize{\myCS}{\text} \myCS \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\myCS{\textit{A}\textit{B}\textit{C}} \scancs[1]{\text}{\myCS} \StrSubstitute*[2]{\text}{textit}{textbf}[\text] \tokenize{\myCS}{\text} \myCS \end{minipage} \subsubsection{Example 2} Let's try to write a macro \verb|\tofrac| that transforms an argument of this type \guill{a/b} into \guill{$\frac{a}{b}$}:\par\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \verbtocs{\csfrac}|\frac|% \verbtocs{\Ob}|{|% \verbtocs{\Cb}|}|% \newcommand\tofrac[1]{% \scancs[0]{\myfrac}{#1}% \StrBefore{\myfrac}{/}[\num]% \StrBehind{\myfrac}{/}[\den]% \tokenize\myfrac{\csfrac\Ob\num\Cb\Ob\den\Cb}% $\myfrac$} \tofrac{15/9} \tofrac{u_{n+1}/u_n} \tofrac{a^m/a^n} \tofrac{x+\sqrt{x}/\sqrt{x^2+x+1}} \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \verbtocs{\csfrac}|\frac|% \verbtocs{\Ob}|{|% \verbtocs{\Cb}|}|% \newcommand\tofrac[1]{% \scancs[0]{\myfrac}{#1}% \StrBefore{\myfrac}{/}[\num]% \StrBehind{\myfrac}{/}[\den]% \tokenize\myfrac{\csfrac\Ob\num\Cb\Ob\den\Cb}% $\myfrac$} \tofrac{15/9} \tofrac{u_{n+1}/u_n} \tofrac{a^m/a^n} \tofrac{x+\sqrt{x}/\sqrt{x^2+x+1}} \end{minipage} \subsubsection{Example 3} In a control sequence \verb|\text|, let's try to write in bold the first word that follows the word "new". In this example, \verb|\text| contains:\par\medskip \hfill\verb|Try the new package xstring !|\hfill{}\bigskip \setverbdelim{|} \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\text{Try the new package xstring !} \def\word{new} \StrBehind[1]{\text}{\word}[\name] \IfBeginWith{\name}{ }% {\StrGobbleLeft{\name}{1}[\name]}% {}% \StrBefore{\name}{ }[\name] \verbtocs{\before}|\textbf{| \verbtocs{\after}|}| \StrSubstitute[1]% {\text}{\name}{\before\name\after}[\text] \tokenize{\text}{\text} \text \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\text{Try the new package xstring !} \def\word{new} \StrBehind[1]{\text}{\word}[\name] \IfBeginWith{\name}{ }% {\StrGobbleLeft{\name}{1}[\name]}% {}% \StrBefore{\name}{ }[\name] \verbtocs{\before}|\textbf{| \verbtocs{\after}|}| \StrSubstitute[1]% {\text}{\name}{\before\name\after}[\text] \tokenize{\text}{\text} \text \end{minipage} \subsubsection{Example 4} A control sequence \verb|\myCS| défined with an \verb|\edef| contains control sequences with their possible arguments. How to reverse the order of the 2 first control sequences? In this example, \verb|\myCS| contains:\par\medskip \hfill\verb|\textbf{A}\textit{B}\texttt{C}|\hfill{}\bigskip We expect a final result containing \verb|\textit{B}\textbf{A}\texttt{C}| and displaying \textit{B}\textbf{A}\texttt{C}\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \def\myCS{\textbf{A}\textit{B}\texttt{C}} \scancs[1]{\text}{\myCS} \verbtocs{\antislash}|\| \StrBefore[3]{\text}{\antislash}[\firsttwo] \StrBehind{\text}{\firsttwo}[\others] \StrBefore[2]{\firsttwo}{\antislash}[\avant] \StrBehind{\firsttwo}{\avant}[\apres]% \tokenize{\myCS}{\apres\avant\others}% result: \myCS \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \def\myCS{\textbf{A}\textit{B}\texttt{C}} \scancs[1]{\text}{\myCS} \verbtocs{\antislash}|\| \StrBefore[3]{\text}{\antislash}[\firsttwo] \StrBehind{\text}{\firsttwo}[\others] \StrBefore[2]{\firsttwo}{\antislash}[\avant] \StrBehind{\firsttwo}{\avant}[\apres]% \tokenize{\myCS}{\apres\avant\others}% result: \myCS \end{minipage} \subsubsection{Example 5} A control sequence \verb|\myCS| defined with an \verb|\edef| contains control sequences and "groups" between braces. Let's try to find the $n$\th{} group, i.e. what is between the $n$\th{} pair of balanced braces. In this example, \verb|\myCS| contains:\par\medskip \hfill\verb|\a{1\b{2}}\c{3}\d{4\e{5}\f{6{7}}}|\hfill{}\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} \newcount\occurr \newcount\nbgroup \newcommand\findgroup[2]{% \scancs[1]{\text}{#2}% \occurr=0 \nbgroup=0 \def\findthegroup{% \StrBehind{\text}{\Obr}[\remain]% \advance\occurr by 1% next "{" \StrBefore[\the\occurr]{\remain}{\Cbr}[\group]% \StrCount{\group}{\Obr}[\nbA]% \StrCount{\group}{\Cbr}[\nbB]% \ifnum\nbA=\nbB% balanced braces ? \advance\nbgroup by 1 \ifnum\nbgroup<#1% not the good group ? \StrBehind{\text}{\group}[\text]% \occurr=0% initialise \text & \occur \findthegroup% do it again \fi \else% unbalanced braces ? % look for next "}" \findthegroup \fi} \findthegroup \group} \verbtocs{\Obr}|{| \verbtocs{\Cbr}|}| \def\myCS{\a{1\b{2}}\c{3}\d{4\e{5}\f{6{7}}}} group 1: \findgroup{1}{\myCS}\par group 2: \findgroup{2}{\myCS}\par group 3: \findgroup{3}{\myCS} \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} \newcount\occurr \newcount\nbgroup \newcommand\findgroup[2]{% \scancs[1]{\text}{#2}% \occurr=0 \nbgroup=0 \def\findthegroup{% \StrBehind{\text}{\Obr}[\remain]% \advance\occurr by 1% next "{" \StrBefore[\the\occurr]{\remain}{\Cbr}[\group]% \StrCount{\group}{\Obr}[\nbA]% \StrCount{\group}{\Cbr}[\nbB]% \ifnum\nbA=\nbB% balanced braces ? \advance\nbgroup by 1 \ifnum\nbgroup<#1% if it's not the sought group \StrBehind{\text}{\group}[\text]% \occurr=0% initialization of \text & \occur \findthegroup% do it again \fi \else% unbalanced braces ? % look for next "}" \findthegroup \fi} \findthegroup \group} \verbtocs{\Obr}|{| \verbtocs{\Cbr}|}| \def\myCS{\a{1\b{2}}\c{3}\d{4\e{5}\f{6{7}}}} groupe 1: \findgroup{1}{\myCS}\par groupe 2: \findgroup{2}{\myCS}\par groupe 3: \findgroup{3}{\myCS} \end{minipage} \medskip Notice that 2 counters, 2 tests and a double recursion are necessary to find the group: one of each to find what "\verb|}|" delimits the end of the current group, and the others to know the number of the group being read.\bigskip\bigskip \begin{center} $\star$ $\star$\quad$\star$ \end{center} \bigskip\bigskip That's all, I hope you will find this package useful !\par Please, send me an email if you find a bug or if you have any idea of improvement\ldots\medskip Christian Tellechea \end{document}