diff options
Diffstat (limited to 'Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex')
-rw-r--r-- | Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex | 571 |
1 files changed, 396 insertions, 175 deletions
diff --git a/Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex b/Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex index 0dd3ecaa4ec..2c09848b6e8 100644 --- a/Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex +++ b/Master/texmf-dist/doc/generic/xstring/xstring_doc_en.tex @@ -1,6 +1,6 @@ % This is xtring_doc_en.tex, the english manual of xstring % -% Christian Tellechea 2008 +% Christian Tellechea 2009 % % email : unbonpetit@gmail.com %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -17,8 +17,8 @@ \documentclass[a4paper,10pt]{article} \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[dvips,colorlinks=true,hyperfootnotes=false,citecolor=black,filecolor=black,linkcolor=blue,urlcolor=blue,bookmarks=false,pdfauthor={Christian Tellechea},pdftitle={xstring},pdfsubject={package for strings of tokens},pdfkeywords={xstring,latex,string},pdfcreator={LaTeX}]{hyperref} +\usepackage[a4paper,dvips,margin=1.9cm]{geometry} \usepackage{amsmath,amssymb} \usepackage{textcomp} \usepackage{moreverb} @@ -28,8 +28,8 @@ \usepackage{xstring} \usepackage[french,english]{babel} \makeatletter -\newcommand*\exemple{\@ifstar{\def\frontiere{|}\@xs@MakeVerb}{\def\frontiere{}\@xs@MakeVerb}} -\long\def\@xs@afterreadverb#1{% +\newcommand*\exemple{\@ifstar{\let\frontiere\textbar\@xs@MakeVerb}{\let\frontiere\@empty\@xs@MakeVerb}} +\def\@xs@afterreadverb#1{% \normalexpandarg \tokenize\cs@resultat{#1}% \fullexpandarg @@ -80,7 +80,7 @@ \begin{minipage}{0.85\linewidth} \noindent \hfill\textbf{\textit{Abstract}}\hfill{}\medskip\par - This package which requires $\varepsilon$-\TeX{}, provides macros manipulating strings contaning chars, control sequences, groups between braces and other special tokens so that the macros may be used for programming purposes. Main features are + This package which requires $\varepsilon$-\TeX{}, provides macros manipulating strings of tokens. For a basic use, tokens can be alphanumeric chars, but the macros can also be useful for manipulating tokens, i.e. \TeX{} code. Main features are: \parindent1.5em \begin{itemize} \item[$\triangleright$] tests: @@ -94,7 +94,8 @@ \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. + \item substring between 2 positions; + \item serach of a group with its identifier. \end{itemize} \item[$\triangleright$] substitution of all, or the $n$ first occurrences of a substring for an other substring; \item[$\triangleright$] calculation of numbers: @@ -102,7 +103,8 @@ \item length of a string; \item position of the $n$\th occurrence of a substring; \item how many times a string contains a substring? - \item comparison of 2 strings: position of the first difference. + \item comparison of 2 strings: position of the first difference; + \item identifier of the group in which a macro made a cut or a search. \end{itemize} \end{itemize} \parindent0pt @@ -112,19 +114,17 @@ \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\footnote{Any email to tell me errors would be appreciated!}, and I hope that the following is comprehensible! \section{Presentation} \subsection{Description} -This extension\footnote{This extension does not require \LaTeX{} and can be compiled with Plain $\varepsilon$-\TeX{}.} provides macros and tests operating on strings of \TeX{} code, 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 - +This extension\footnote{This extension does not require \LaTeX{} and can be compiled with Plain $\varepsilon$-\TeX{}.} provides macros and tests operating on strings of tokens, i.e. \TeX{} code, 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 \Xstring reads the arguments of the macros \US by \US\footnote{In the \TeX{} code, a \US is a control sequence, a group between brace or a single char. See also page~\pageref{developpementarguments}.} : when \USs are \guill{simple} chars (catcode 10, 11 and 12), \Xstring logically read the argument char by char. \Xstring can also be used for programming purpose, including in arguments other tokens such as control sequences, braces and tokens with other catcodes. See chapter on reading mode and arguments expansion (page~\pageref{developpementarguments}), the command \verb|\verbtocs| (page~\pageref{verbtocs}) and the command \verb|\scancs| (page~\pageref{scancs}).\medskip -As the arguments may contain chars, advanced users could have problems with catcodes leading to unexpected behaviours. These behaviours can be controlled: read page~\pageref{macrosetoilees}.\medskip +As the arguments may contain any token, advanced users could have problems with catcodes leading to unexpected behaviours. These behaviours can be controlled: read page~\pageref{macrosetoilees}.\medskip Certainly, other packages exist (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. @@ -134,7 +134,11 @@ I decided to write this package of macros because I have never really found tool 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. This package is my first one as I recently discoverd \LaTeX{}\footnote{In november 2007, I will be a noob for a long time\ldots}, so my main motivation was to make progress in programming with \TeX, and to tackle its specific methods. \section{The macros} +\textbf{Important}: in the following, a \argu{number} can be an integer written with numeric chars, a counter, or the result of an arithmetic operation made with the command \verb|\numexpr|. + \subsection{Presentation of macros} +For a better understanding, let's see first the macros with the simpler arguments possible. No special catcode, no exotic token, no control sequence neither: only alphanumeric chars will be contained in the arguments.\medskip + In the following chapters, all the macros will be presented this plan: \begin{itemize} \item the syntax\footnote{The optional star, the optional argument in last position will be explained later. See page~\pageref{macrosetoilees} for starred macros and page~\pageref{argumentoptionnel} for the optional argument.} and the value of optional arguments @@ -248,7 +252,7 @@ Tests if \argu{string} ends with \argu{stringA}, and runs \argu{true} if so, and \subsubsection{\ttfamily\textbackslash IfInteger} -\verb|\IfInteger|\etoile\ARGU{number}\ARGU{true}\ARGU{false} +\verb|\IfInteger|\ARGU{number}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{number} is an integer, and runs \argu{true} if so, and \argu{false} otherwise.\smallskip @@ -267,7 +271,7 @@ If test is false because unexpected characters, the control sequence \verb|\@xs@ \subsubsection{\ttfamily\textbackslash IfDecimal}\label{ifdecimal} -\verb|\IfDecimal|\etoile\ARGU{number}\ARGU{true}\ARGU{false} +\verb|\IfDecimal|\ARGU{number}\ARGU{true}\ARGU{false} \smallskip Tests if \argu{number} is a decimal, and runs \argu{true} if so, and \argu{false} otherwise.\smallskip @@ -302,7 +306,7 @@ If test is false because unexpected characters, the control sequence \verb|\@xs@ \verb|\IfStrEq|\etoile\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip -Tests if the strings \argu{stringA} and \argu{stringB} are equal, i.e. if they contain successively the same characters in the same order. Runs \argu{true} if so, and \argu{false} otherwise.\smallskip +Tests if the strings \argu{stringA} and \argu{stringB} are equal, i.e. if they contain successively the same \USs in the same order. Runs \argu{true} if so, and \argu{false} otherwise.\smallskip \exemple|\IfStrEq{a1b2c3}{a1b2c3}{true}{false}| \exemple|\IfStrEq{abcdef}{abcd}{true}{false}| @@ -315,7 +319,7 @@ Tests if the strings \argu{stringA} and \argu{stringB} are equal, i.e. if they c \subsubsection{\ttfamily\textbackslash IfEq} -\verb|\IfEq|\etoile\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} +\verb|\IfEq|\ARGU{stringA}\ARGU{stringB}\ARGU{true}\ARGU{false} \smallskip Tests if the strings \argu{stringA} and \argu{stringB} are equal, \emph{except} if both \argu{stringA} and \argu{stringB} contain numbers in which case the macro tests if these numbers are equal. Runs \argu{true} if so, and \argu{false} otherwise.\smallskip @@ -323,8 +327,7 @@ Tests if the strings \argu{stringA} and \argu{stringB} are equal, \emph{except} \begin{Conditions} \item The definition of \emph{number} is given with the macro \verb|IfDecimal| (see page~\pageref{ifdecimal}), and thus : \item \guill{+} signs are optional; - \item Decimal separator can be a dot or a comma; - \item It is possible to evaluate an algebric expression with the $\varepsilon$-\TeX{} primitive \verb|\numexpr|, keeping in mind that it operate on integers only, and that results of divisions are \emph{rounded} and not truncated! + \item Decimal separator can be a dot or a comma. \end{Conditions} \exemple|\IfEq{a1b2c3}{a1b2c3}{true}{false}| @@ -352,7 +355,7 @@ Tests if the strings \argu{stringA} and \argu{stringB} are equal, \emph{except} \end{minipage} \smallskip -Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IfStrEq| (see above). If the test $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip +Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IfStrEq| (see above). If the test number $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip \exemple|\IfStrEqCase{b}{{a}{AA}{b}{BB}{c}{CC}}| \exemple*|\IfStrEqCase{abc}{{a}{AA}{b}{BB}{c}{CC}}| @@ -372,7 +375,7 @@ Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, \end{minipage} \smallskip -Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IEq| (see above). If the test $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip +Tests successively if \argu{string} is equal to \argu{string1}, \argu{string2}, etc. Comparison is made with \verb|\IEq| (see above). If the test number $i$ is positive (the \argu{string} matches \argu{string\;$i$}), the macro runs \argu{code\;$i$} and ends. If all tests fail, the macro runs the optional \argu{other cases code}, if present.\smallskip \exemple|\IfEqCase{b}{{a}{AA}{b}{BB}{c}{CC}}| \exemple*|\IfEqCase{abc}{{a}{AA}{b}{BB}{c}{CC}}| @@ -460,7 +463,7 @@ In \argu{string}, returns the substring between\footnote{In a strict sense, i.e. \subsubsection{\ttfamily\textbackslash StrSubstitute} -\verb|\StrSubstitute|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} +\verb|\StrSubstitute|\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip The value of the optional argument \argu{number} is 0 by default.\par\smallskip @@ -508,31 +511,8 @@ Delete the \argu{number} first occurrences of \argu{stringA} in \argu{string}, e \exemple|\StrDel[9]{abracadabra}{a}| \exemple|\StrDel{a bc def }{ }| -\subsubsection{\ttfamily\textbackslash StrSplit} -\verb|\StrSplit|\etoile\ARGU{string}\ARGU{number}\ARGU{csA}\ARGU{csB} -\smallskip - -The \argu{string}, is splitted after the character at position \argu{number}. The left part is assigned to the control sequence \argu{csA} and the right part is assigned to \argu{csB}.\par -This macro returns two strings, so it does \emph{not} display anything. Consequently, it does not provide the optional argument in last position.\medskip - -\begin{Conditions} - \item If \argu{number}${}\leqslant0$, \argu{csA} is empty and \argu{csB} is equal to \argu{string}; - \item If \argu{number}${}\geqslant$\argu{lengthString}, \argu{csA} is equal to \argu{string} and \argu{csB} is empty; - \item If \argu{string} is empty, \argu{csA} and \argu{csB} are empty, whatever be the integer \argu{number}. -\end{Conditions} - -\setverbdelim{=} -\exemple=\StrSplit{abcdef}{4}{\aa}{\bb}results: |\aa| and |\bb|= -\exemple=\StrSplit{a b c }{2}{\aa}{\bb}results: |\aa| and |\bb|= -\exemple=\StrSplit{abcdef}{1}{\aa}{\bb}results: |\aa| and |\bb|= -\exemple=\StrSplit{abcdef}{5}{\aa}{\bb}results: |\aa| and |\bb|= -\exemple=\StrSplit{abcdef}{9}{\aa}{\bb}results: |\aa| and |\bb|= -\exemple=\StrSplit{abcdef}{-3}{\aa}{\bb}results: |\aa| and |\bb|= -\setverbdelim{|} - \subsubsection{\ttfamily\textbackslash StrGobbleLeft} - -\verb|\StrGobbleLeft|\etoile\ARGU{string}\ARGU{number}\arguC{name} +\verb|\StrGobbleLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} first characters on the left.\medskip @@ -551,7 +531,7 @@ In \argu{string}, delete the \argu{number} first characters on the left.\medskip \subsubsection{\ttfamily\textbackslash StrLeft} -\verb|\StrLeft|\etoile\ARGU{string}\ARGU{number}\arguC{name} +\verb|\StrLeft|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} first characters on the left.\medskip @@ -570,7 +550,7 @@ In \argu{string}, returns the \argu{number} first characters on the left.\medski \subsubsection{\ttfamily\textbackslash StrGobbleRight} -\verb|\StrGobbleRight|\etoile\ARGU{string}\ARGU{number}\arguC{name} +\verb|\StrGobbleRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, delete the \argu{number} last characters on the right.\medskip @@ -583,7 +563,7 @@ In \argu{string}, delete the \argu{number} last characters on the right.\medskip \subsubsection{\ttfamily\textbackslash StrRight} -\verb|\StrRight|\etoile\ARGU{string}\ARGU{number}\arguC{name} +\verb|\StrRight|\ARGU{string}\ARGU{number}\arguC{name} \smallskip In \argu{string}, returns the \argu{number} last characters on the right.\medskip @@ -599,7 +579,7 @@ In \argu{string}, returns the \argu{number} last characters on the right.\medski \verb|\StrChar|\etoile\ARGU{string}\ARGU{number}\arguC{name} \smallskip -Returns the character at the position \argu{number} in \argu{string}.\medskip +Returns the \US at the position \argu{number} in \argu{string}.\medskip \begin{Conditions} \item If \argu{string} is empty, no caracter is returned; @@ -613,7 +593,7 @@ Returns the character at the position \argu{number} in \argu{string}.\medskip \subsubsection{\ttfamily\textbackslash StrMid} -\verb|\StrMid|\etoile\ARGU{string}\ARGU{numberA}\ARGU{numberB}\arguC{name} +\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 @@ -634,10 +614,10 @@ In \argu{string}, returns the substring between\footnote{In the broad sense, i.e \exemple|\StrMid{xstring}{3}{3}| \exemple*|\StrMid{a bc def }{2}{7}| -\subsection{Number results} +\subsection{Macros returning a number} \subsubsection{\ttfamily\textbackslash StrLen} -\verb|\StrLen|\etoile\ARGU{string}\arguC{name} +\verb|\StrLen|\ARGU{string}\arguC{name} \smallskip Return the length of \argu{string}. @@ -648,7 +628,7 @@ Return the length of \argu{string}. \subsubsection{\ttfamily\textbackslash StrCount} -\verb|\StrCount|\etoile\ARGU{string}\ARGU{stringA}\arguC{name} +\verb|\StrCount|\ARGU{string}\ARGU{stringA}\arguC{name} \smallskip Counts how many times \argu{stringA} is contained in \argu{string}.\par\medskip @@ -664,7 +644,7 @@ Counts how many times \argu{stringA} is contained in \argu{string}.\par\medskip \subsubsection{\ttfamily\textbackslash StrPosition} -\verb|\StrPosition|\etoile\arguC{number}\ARGU{string}\ARGU{stringA}\arguC{name} +\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 @@ -689,11 +669,11 @@ In \argu{string}, returns the position of the \argu{number}\th occurrence of \ar \verb|\StrCompare|\etoile\ARGU{stringA}\ARGU{stringB}\arguC{name} \smallskip -This macro has 2 tolerances: the "normal" tolerance, used bu default, and the "strict" tolerance. +This macro has 2 tolerances: the "normal" tolerance, used by default, and the "strict" tolerance. \begin{itemize} - \item The normal tolerance, activated with \verb|\comparenormal|.\par + \item The normal tolerance is activated with \verb|\comparenormal|.\par The macro compares characters from left to right in \argu{stringA} and \argu{stringB} until a difference appears or the end of the shortest string is reached. The position of the first difference is returned and if no difference is found, the macro return 0. - \item The strict tolerance, activated with \verb|\comparestrict|.\par + \item The strict tolerance is activated with \verb|\comparestrict|.\par The macro compares the 2 strings. If they are equal, it returns 0. If not, the position of the first difference is returned. \end{itemize} \medskip @@ -728,15 +708,58 @@ Examples with the strict tolerance:\par\comparestrict \subsection{Expansion of arguments} \label{devarg} \subsubsection{The commands {\ttfamily \textbackslash fullexpandarg}, {\ttfamily \textbackslash expandarg} and {\ttfamily \textbackslash noexpandarg}} -The command \verb|\fullexpandarg| is called by default, so all the arguments are fully expanded (an \verb|\edef| is used) before the the macro is called. In most of the cases, this expansion mode avoids chains of \verb|\expandafter| and allows lighter code. +The command \verb|\fullexpandarg| is called by default, so all the arguments are fully expanded (an \verb|\edef| is used) before the the macro works on them. In most of the cases, this expansion mode avoids chains of \verb|\expandafter| and allows lighter code. Of course, the expansion of argument can be canceled to find back the usual behaviour of \TeX{} with the comands \verb|\noexpandarg| or \verb|\normalexpandarg|.\medskip -An other expansion mode can be called with \verb|\expandarg|. In this case, the \textbf{first token} of each argument is expanded \emph{one time} while all other tokens are left unchanged (for the expansion of all tokens one time, you should call the macro \verb|\scancs*|, see page~\pageref{scancs}).\medskip +Another expansion mode can be called with \verb|\expandarg|. In this case, the \textbf{first token} of each argument is expanded \emph{one time} while all other tokens are left unchanged (if you want the expansion of all tokens one time, you should call the macro \verb|\scancs*|, see page~\pageref{scancs}).\medskip + +The commands \verb|\fullexpandarg|, \verb|\noexpandarg|, \verb|\normalexpandarg| and \verb|\expandarg| can be called at any moment in the code; they behave as "switches" and they can be locally used in a group.\medskip -The commands \verb|\fullexpandarg|, \verb|\noexpandarg|, \verb|\normalexpandarg| and \verb|\expandarg| can be called at any moment in the code; they behave as "switches". They can be locally used in a group.\medskip +It is possible to save the expansion mode with \verb|\saveexpandmode|, then modify this expansion mode and come back to the situation when it was saved with \verb|\restoreexpandmode|.\medskip -It is possible to save the expansion mode with \verb|\saveexpandmode|, then modify this expansion mode and come back to the situation when it was saved with \verb|\restoreexpandmode|. +In the following list, for every macro of the previous chapter, the arguments colored in \textcolor{red}{red} will possibly be expanded, according to the expansion mode:\smallskip + +\newcommand\colorise{\color{red}} +\parindent3em +\begin{itemize} + \item\verb|\IfSubStr|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfSubStrBefore|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfSubStrBehind|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfBeginWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfEndWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfInteger|{\colorise\ARGU{number}}\ARGU{true}\ARGU{false} + \item\verb|\IfDecimal|{\colorise\ARGU{number}}\ARGU{true}\ARGU{false} + \item\verb|\IfStrEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfStrEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par + \setbox0=\hbox{{\ttfamily\string\IfStrEqCase}\etoile\ARGU{string}{\ttfamily\{}} + \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par + \hskip\wd0\ \ldots\par + \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} + \item\verb|\IfEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par + \setbox0=\hbox{{\ttfamily\string\IfEqCase}\etoile\ARGU{string}{\ttfamily\{}} + \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par + \hskip\wd0\ \ldots\par + \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} + \item\verb|\StrBefore|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrBehind|\etoile{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrBetween|\etoile{\colorise\arguCC{number1}{number2}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} + \item\verb|\StrSubstitute|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} + \item\verb|\StrDel|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrSplit|{\colorise\ARGU{string}\ARGU{number}}\ARGU{stringA}\ARGU{stringB}\qquad{\small(see macro \verb|StrSplit| page~\pageref{StrSplit})} + \item\verb|\StrGobbleLeft|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} + \item\verb|\StrLeft|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} + \item\verb|\StrGobbleRight|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} + \item\verb|\StrRight|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} + \item\verb|\StrChar|{\colorise\ARGU{string}\ARGU{number}}\arguC{name} + \item\verb|\StrMid|{\colorise\ARGU{string}\ARGU{number1}\ARGU{number2}}\arguC{name} + \item\verb|\StrLen|{\colorise\ARGU{string}}\arguC{name} + \item\verb|\StrCount|{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrPosition|{\colorise\arguC{number}\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrCompare|{\colorise\ARGU{stringA}\ARGU{stringB}}\arguC{name} +\end{itemize} +\parindent0pt \subsubsection{Chars allowed in arguments} First of all, whatever be the current expansion mode, \textbf{tokens with catcode 6 and 14 (usually {\ttfamily\#} and {\ttfamily\%}) are forbidden in all the arguments}\footnote{Maybe, the token {\ttfamily\#} will be allowed in a further version.}.\bigskip @@ -763,28 +786,28 @@ When expansion is not full (\verb|\expandarg| or \verb|\noexpandarg| are active) \subsection{Expansion of macros, optional argument} \label{argumentoptionnel} -The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \verb|\edef|. Nestling macros is not possible neither, even with \verb|\expandafter|.\medskip +The macros of this package are not purely expandable, i.e. they cannot be put in the argument of an \verb|\edef|. Nestling macros is not possible neither.\medskip -For this reason, all the macros returning a result (i.e. all excepted the tests and \verb|\StrSplit|) have an optional argument in last position. The syntax is \arguC{name}, where \argu{name} 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{name} purely expandable. Of course, if an optional argument is present, the macro does not display anything.\medskip +For this reason, all the macros returning a result (i.e. all excepted the tests) have an optional argument in last position. The syntax is \arguC{name}, where \argu{name} 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{name} purely expandable. Of course, if an optional argument is present, the macro does not display anything.\medskip Thus, this structure not allowed, supposed to assign to \verb|\Result| the 4 chars on the left of \verb|xstring|:\par \hspace{0.2\linewidth}\verb|\edef\Result{\StrLeft{xstring}{4}}|\par \qquad is equivalent to :\par \hspace{0.2\linewidth}\verb|\StrLeft{xstring}{4}[\Result]|\medskip -And this not allowed nested structure, supposed to remove the first and last char of \verb|xstring|:\par +And this not allowed nested structure, supposed to remove the first and last char of \verb|xstring|:\par \hspace{0.2\linewidth}\verb|\StrGobbleLeft{\StrGobbleRight{xstring}{1}}{1}|\par -\qquad can be written like this:\par +\qquad should be written like this:\par \hspace{0.2\linewidth}\verb|\StrGobbleRight{xstring}{1}[\mystring]|\par \hspace{0.2\linewidth}\verb|\StrGobbleleft{\mystring}{1}|\par \subsection{How does \Xstring read the arguments?} \label{developpementarguments} \subsubsection{Syntax unit by \US} -The macros of \Xstring read their arguments \US par \US. In the \TeX{} code, a \US is either:\parindent3em +The macros of \Xstring read their arguments \US par \US. In the \TeX{} code, a \US\footnote{For advanced users used to \LaTeX{} programming, a \US is what is gobbled by the macro \texttt{\string\@gobble} whose code is: {\ttfamily\string\def\string\@gobble\string#1\string{\string}}} is either:\parindent3em \begin{itemize} \item a control sequence; - \item a group, i.e. what is between 2 balanced braces; + \item a group, i.e. what is between 2 balanced braces (usually tokens catcode 1 and 2); \item a char. \end{itemize} \medskip\parindent0pt @@ -793,7 +816,7 @@ Let's see what is a \US with an example. Let's take this argument : "\verb|ab\te It has 6 \USs: "\verb|a|", "\verb|b|", "\verb|\textbf|", "\verb|{xyz}|", "\verb|c|" and "\verb|d|".\medskip -What will happen while \verb|\noexpandarg| is active, we ask \Xstring to find the length of this argument and find its 4\th "char'?\medskip +What will happen if, while \verb|\noexpandarg| is active, we ask \Xstring to find the length of this argument and find its 4\th "char"\medskip \begin{minipage}[r]{0.6\linewidth} \hfill @@ -814,14 +837,14 @@ What will happen while \verb|\noexpandarg| is active, we ask \Xstring to find th \fullexpandarg \medskip -It is necessary to use \verb|\meaning| to see the real expansion of \verb|\mychar|, and not simply call \verb|\mychar| which make loose informations (braces here). We do not obtain a "char" but a \US, as expected. +It is necessary to use \verb|\meaning| to see the real expansion of \verb|\mychar|, and not simply call \verb|\mychar| to display it, which make loose informations (braces here). We do not obtain a "char" but a \US, as expected. \subsubsection{Exploration of groups} -By default, the command \verb|\noexploregroups| is called, so in the main argument, \Xstring does not look into groups, and simply consider them as \US. +By default, the command \verb|\noexploregroups| is called, so in the argument containing the string of tokens, \Xstring does not look into groups, and simply consider them as a \US. -For specific uses, it can be necessary to look into groups: \verb|\exploregroups| changes the exploration mode an makes the macros look inside groups.\medskip +For specific uses, it can be necessary to look into groups: \verb|\exploregroups| changes the exploration mode and forces the macros to look inside groups.\medskip -What does this exploration mode in the preceding example? \Xstring does not count the group as a single \US but look inside it and counts the \US found inside (\verb|x|, \verb|y| and \verb|z|), and so on if there were several nested groups:\smallskip +What does this exploration mode in the previous example? \Xstring does not count the group as a single \US but looks inside it and counts the \US found inside (\verb|x|, \verb|y| and \verb|z|), and so on if there were several nested groups:\smallskip \begin{minipage}[r]{0.6\linewidth} \hfill @@ -844,7 +867,7 @@ What does this exploration mode in the preceding example? \Xstring does not coun \fullexpandarg\noexploregroups \medskip -Exploring the groups can be usefull for counting a substring in a string (\verb|\StrCount|), for the position of a substring in a string (\verb|\StrPosition|) or for tests, but has a severe limitation with macros returning a string: when an argument is cut inside a group, \textbf{the result does not take into account what is outside this group}. This exploration mode must be used knowingly this limitation when calling macros returning a string.\smallskip +Exploring the groups can be usefull for counting a substring in a string (\verb|\StrCount|), for the position of a substring in a string (\verb|\StrPosition|) or for tests, but has a severe limitation with macros returning a string: when a string is cut inside a group, \textbf{the result does not take into account what is outside this group}. This exploration mode must be used knowingly this limitation when calling macros returning a string.\smallskip Let's see what this means with an example. We want to know what is on the left of the second appearance of \verb|\a| in the argument \verb|\a1{\b1\a2}\a3|. As groups are explored, this appearance is inside this group : \verb|{\b1\a2}|. The result will be \verb|\b1|. Let's check:\smallskip @@ -871,19 +894,17 @@ Exploring the groups\footnote{The file test of \Xstring has many examples underl Moreover, 2 macros run in \verb|\noexploregroups| mode, whatever be the current mode: \verb|\StrBetween| et \verb|\StrMid|.\medskip -It is possible to save the "explore mode" with \verb|\saveexploremode|, then modify this "explore mode" and come back to the situation when it was saved with \verb|\restoreexploremode|. +It is possible to save the exploration mode with \verb|\saveexploremode|, then modify it and come back to the situation when it was saved with \verb|\restoreexploremode|. -\subsection{Catcode of arguments, starred macros} +\subsection{Catcode and starred macros} \label{macrosetoilees} -Macros of this package take the catcodes of characters into account. To avoid unexpected behaviour (particulary with tests), you should keep in mind that characters \emph{and their catcodes} are examined.\medskip +Macros of this package take the catcodes of tokens into account. To avoid unexpected behaviour (particulary with tests), you should keep in mind that tokens \emph{and their catcodes} are examined.\medskip For instance, these two arguments:\par\medskip \hfil\verb|{\string a\string b}|\qquad and\qquad\verb|{ab}|\hfil{}\par\smallskip -do \emph{not} expand into equal strings for xstring! Because of the command \verb|\string|, the first expands into "\verb|ab|" with catcodes 12 while the second have characters with their natural catcodes 11. Catcodes do not match! It is necessary to be aware of this, particulary with \TeX{} commands like \verb|\string| whose expansions are a strings with char catcodes 12 and 10 : \verb|\detokenize|, \verb|\meaning|, \verb|\jobname|, \verb|\fontname|, \verb|\romannumeral|, etc.\medskip +do \emph{not} expand into equal strings for xstring! Because of the command \verb|\string|, the first expands into "\verb|ab|" with catcodes 12 while the second have characters with their natural catcodes 11. Catcodes do not match! It is necessary to be aware of this, particulary with \TeX{} commands like \verb|\string| whose expansions are a strings with chars catcodes 12 and 10 : \verb|\detokenize|, \verb|\meaning|, \verb|\jobname|, \verb|\fontname|, \verb|\romannumeral|, etc.\medskip -Starred macros do not take catcodes into account. They simply convert their textual arguments into arguments with catcodes 10, 11 and 12, and call the non-starred macros with these modified arguments.\medskip - -\textbf{Warning}: the use of a strarred macro has consequences! The arguments are \guill{detokenized}, thus, there is no more control sequences, groups, neither any special char: everything is converted into chars with "harmless" catcodes.\medskip +Starred macros do not take catcodes into account. They simply convert some arguments into arguments with catcodes 10, 11 and 12, and call the non-starred macros with these modified arguments. The optional arguments are not modified and the catcodes are left unchanged.\medskip Here is an example:\par\smallskip \begin{minipage}[r]{0.6\linewidth} @@ -899,26 +920,228 @@ Here is an example:\par\smallskip \IfStrEq*{\string a\string b}{ab}{true}{false} \end{minipage}\smallskip -In the example above, after expansion (assumed that \verb|\fullexpandarg| is active), the first argument contains \verb|{ab}| where, because of \verb|\string|, both chars have catcode 12. The second argument is \verb|{ab}| where both chars have their natural catcode 11. Strings are not equal because of the catcode and the test is negative, if the unstarred version is used.\bigskip +The strings do not match because of catcode differences: the test is negative in the non-starred macro.\bigskip -For the macros returning a string, if the starred version is used, the result will be a string in which chars have catcodes 12 and 10 for space. For example, after a "\verb|\StrBefore*{a \b c d}{c}[\mytext]|", the control sequence \verb|\mytext| expands to "\verb|a|${}_{12}$\verb*| |${}_{10}$\verb|\|${}_{12}$\verb|b|${}_{12}$\verb*| |${}_{10}$". +\textbf{Warning}: the use of a strarred macro has consequences! The arguments are \guill{detokenized}, thus, there is no more control sequences, groups, neither any special char: everything is converted into chars with "harmless" catcodes.\medskip + +For the macros returning a string, if the starred version is used, the result will be a string in which chars have catcodes 12 and 10 for space. For example, after a "\verb|\StrBefore*{a \b c d}{c}[\mytext]|", the control sequence \verb|\mytext| expands to "\verb|a|${}_{12}$\verb*| |${}_{10}$\verb|\|${}_{12}$\verb|b|${}_{12}$\verb*| |${}_{10}$".\medskip + +The macro with a starred version are listed below. For these macros, if starred version is used, the \textcolor{red}{red} arguments will be detokenized:\smallskip -\section{Other macros for assistance in programming} +\parindent3em +\begin{itemize} + \item\verb|\IfSubStr|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfSubStrBefore|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfSubStrBehind|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfBeginWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfEndWith|\etoile{\colorise\ARGU{string}\ARGU{stringA}}\ARGU{true}\ARGU{false} + \item\verb|\IfStrEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfEq|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\ARGU{true}\ARGU{false} + \item\verb|\IfStrEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par + \setbox0=\hbox{{\ttfamily\string\IfStrEqCase}\etoile\ARGU{string}{\ttfamily\{}} + \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par + \hskip\wd0\ \ldots\par + \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} + \item\verb|\IfEqCase|\etoile{\colorise\ARGU{string}\texttt{\color{black}\{}\colorise\ARGU{string1}}\ARGU{code1}\par + \setbox0=\hbox{{\ttfamily\string\IfEqCase}\etoile\ARGU{string}{\ttfamily\{}} + \hskip\wd0{\colorise\ARGU{string2}}\ARGU{code2}\par + \hskip\wd0\ \ldots\par + \hskip\wd0{\colorise\ARGU{string $n$}}\ARGU{code $n$}\verb|}|\arguC{other cases code} + \item\verb|\StrBefore|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrBehind|\etoile\arguC{number}{\colorise\ARGU{string}\ARGU{stringA}}\arguC{name} + \item\verb|\StrBetween|\etoile\arguCC{number1}{number2}{\colorise\ARGU{string}\ARGU{stringA}\ARGU{stringB}}\arguC{name} + \item\verb|\StrCompare|\etoile{\colorise\ARGU{stringA}\ARGU{stringB}}\arguC{name} +\end{itemize} +\parindent0pt + +\section{Advanced macros for programming} Though \Xstring is able to read arguments containing \TeX{} or \LaTeX{} code, for some advanced programming needs, it can be insufficient. This chapter presents other macros able to get round some limitations. +\subsection{Finding a group, macros {\ttfamily\textbackslash StrFindGroup} and {\ttfamily\textbackslash groupID}} +When \verb=\exploregroups= mode is active, the macro \verb=\StrFindGroup= finds a group between braces with its identifier:\smallskip + +\verb|\StrFindGroup|\ARGU{argument}\ARGU{identifier}\arguC{name}\medskip + +When the group matching the identifier does not exist, an empty string is assigned to \argu{name}. If the group is found, this group \emph{with its braces} is assigned to \argu{name}.\smallskip + +This identifier characterizes the nestling position of the group. It is a list of one or several integers separated with commas. $n_1$, the first integer is the number of the group (not nestled in another) in which the sought group is. Inside this group, the second integer $n_2$ is the number of the group (not nestled in another) in which the sought group is\ldots and so on until the necessary nestling depth is reached to obtain the sougth after group.\bigskip + +Let's take an example with 3 levels of nestled groups. In this example, braces delimiting groups are colored in red for nestling level 1, in blue for level 2 and in green for level 3. The groups are numbered with the rule seen above: +\begingroup + \def\AccO#1{\text{\color{#1}\ttfamily\{}} + \def\AccF#1{\text{\color{#1}\ttfamily\}}} + \def\texte#1{\text{\texttt{\color{black}#1}}} + \def\decalbrace{\vphantom{\underbrace{A}_A}} + \[\texte{a}{\color{red}\underbrace{\AccO{red}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{bc}\AccF{blue}}_{\color{blue}1}}\texte{d}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{efg}\AccF{blue}}_{\color{blue}2}}\AccF{red}}_{\color{red}1}}\texte{h}{\color{red}\underbrace{\AccO{red}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{ij}\AccF{blue}}_{\color{blue}1}}{\color{blue}\underbrace{\decalbrace\AccO{blue}\texte{k}\AccF{blue}}_{\color{blue}2}}{\color{blue}\underbrace{\AccO{blue}\texte{l}{\color{green}\underbrace{\AccO{green}\texte{m}\AccF{green}}_{\color{green}1}}{\color{green}\underbrace{\AccO{green}\texte{no}\AccF{green}}_{\color{green}2}}\AccF{blue}}_{\color{blue}3}}\texte{p}\AccF{red}}_{\color{red}2}}\] +\endgroup +\smallskip +In this example:\parindent3em +\begin{itemize} + \item the group \verb={{bc}d{efg}}= has the identifier: \texttt{\color{red}1} + \item the group \verb={ij}= has the identifier: \texttt{{\color{red}2},\color{blue}1} + \item the group \verb={no}= has the identifier: \texttt{{\color{red}2},{\color{blue}3},\color{green}2} + \item the whole argument \verb=a{{bc}d{efg}}h{{ij}{k}{l{m}{no}}p}= has the identifier \verb=0=, only case where the integer 0 is appears in the identifier of a group. +\end{itemize} +\parindent0pt +\medskip +Here is the full example:\smallskip + +\begin{minipage}[r]{0.6\linewidth} +\hfill +\begin{boxedverbatim} + \exploregroups + \expandarg + \def\string{a{{bc}d{efg}}h{{ij}{k}{l{m}{no}}p}} + \StrFindGroup{\string}{1}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,1}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,3,2}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,5}[\mygroup] + \meaning\mygroup\par +\end{boxedverbatim} +\hspace*{0.3cm} +\end{minipage}% +\begin{minipage}[r]{0.3\linewidth} + \exploregroups + \expandarg + \def\string{a{{bc}d{efg}}h{{ij}{k}{l{m}{no}}p}} + \StrFindGroup{\string}{1}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,1}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,3,2}[\mygroup] + \meaning\mygroup\par + \StrFindGroup{\string}{2,5}[\mygroup] + \meaning\mygroup\par +\end{minipage} +\bigskip + +The reverse process exists, and several macros of \Xstring provide the identifier of the group in which they made a cut or they found a substring. These macros are: \verb|\IfSubStr|, \verb|\StrBefore|, \verb|\StrBehind|, \verb|\StrSplit|, \verb|\StrLeft|, \verb|\StrGobbleLeft|, \verb|\StrRight|, \verb|\StrGobbleRight|, \verb|\StrChar|, \verb|\StrPosition|.\medskip + +After these macros, the control sequence \verb|\groupID| expands to the identifier of the group where the cut has been done or the search has succeeded. When not cut can be done or the search fails, \verb|\groupID| is empty. Obvioulsly, the use of \verb|\groupID| has sense only when \verb|\exploregroups| mode is acive and when non starred macros are used.\smallskip + +Here are some examples with the macro \verb|\StrChar|:\smallskip + +\begin{minipage}[r]{0.6\linewidth} +\hfill +\begin{boxedverbatim} + \exploregroups + char no 1 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{1}\qquad + \string\groupID = \groupID\par + char no 4 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{4}\qquad + \string\groupID = \groupID\par + char no 6 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{6}\qquad + \string\groupID = \groupID\par + char no 20 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{20}\qquad + \string\groupID = \groupID +\end{boxedverbatim} +\hspace*{0.3cm} +\end{minipage}% +\begin{minipage}[r]{0.3\linewidth} + \exploregroups + char no 1 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{1}\qquad + \string\groupID = \groupID\par + char no 4 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{4}\qquad + \string\groupID = \groupID\par + char no 6 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{6}\qquad + \string\groupID = \groupID\par + char no 20 = \StrChar{a{b{cd}{e{f}g}h}ijkl}{20}\qquad + \string\groupID = \groupID +\end{minipage} + +\subsection{Splitting a string, the macro {\ttfamily\textbackslash StrSplit}} +\label{StrSplit} +Here is the syntax:\smallskip + +\verb|\StrSplit|\ARGU{string}\ARGU{number}\ARGU{stringA}\ARGU{stringB} +\smallskip + +The \argu{string}, is splitted after the \US at position \argu{number}. The left part is assigned to the control sequence \argu{stringA} and the right part is assigned to \argu{stringB}.\par +This macro returns two strings, so it does \emph{not} display anything. Consequently, it does not provide the optional argument in last position.\medskip + +\begin{Conditions} + \item If \argu{number}${}\leqslant0$, \argu{stringA} is empty and \argu{stringB} is equal to \argu{string}; + \item If \argu{number}${}\geqslant$\argu{lengthString}, \argu{stringA} is equal to \argu{string} and \argu{stringB} is empty; + \item If \argu{string} is empty, \argu{stringA} and \argu{stringB} are empty, whatever be the integer \argu{number}. +\end{Conditions} + +\setverbdelim{=} +\exemple=\StrSplit{abcdef}{4}{\aa}{\bb}results: |\aa| and |\bb|= +\exemple=\StrSplit{a b c }{2}{\aa}{\bb}results: |\aa| and |\bb|= +\exemple=\StrSplit{abcdef}{1}{\aa}{\bb}results: |\aa| and |\bb|= +\exemple=\StrSplit{abcdef}{5}{\aa}{\bb}results: |\aa| and |\bb|= +\exemple=\StrSplit{abcdef}{9}{\aa}{\bb}results: |\aa| and |\bb|= +\exemple=\StrSplit{abcdef}{-3}{\aa}{\bb}results: |\aa| and |\bb|= +\setverbdelim{|}\medskip + +When the exploration of groups is active and the cut is made at the end of a group, the content of the left string will tbe the entire group while the right string will be empty. The example shows this:\smallskip + +\begin{minipage}[r]{0.6\linewidth} +\hfill +\begin{boxedverbatim} + \exploregroups + \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB +\end{boxedverbatim} +\hspace*{0.3cm} +\end{minipage}% +\begin{minipage}[r]{0.3\linewidth} + \exploregroups + \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB +\end{minipage} +\medskip + +This macro provides a star version: in this case, the cut is made just before the \US which follows the \US at position \argu{number}. Both version give same results, except when the cut is made at the end of a group; in that case, \verb|\StrSplit| closes as many group as necessary until it finds the next \US: the cut is made just before this \US.\smallskip + +\begin{minipage}[r]{0.6\linewidth} +\hfill +\begin{boxedverbatim} + \exploregroups + With natural macro:\par + \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB\par + \string\groupID = \groupID\par + With star macro:\par + \StrSplit*{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB\par + \string\groupID\ = \groupID +\end{boxedverbatim} +\hspace*{0.3cm} +\end{minipage}% +\begin{minipage}[r]{0.3\linewidth} + \exploregroups + With natural macro:\par + \StrSplit{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB\par + \string\groupID = \groupID\par + With star macro:\par + \StrSplit*{ab{cd{ef}gh}ij}{6}\strA\strB + \meaning\strA\par + \meaning\strB\par + \string\groupID\ = \groupID +\end{minipage} + \subsection{Assign a verb content, the macro \ttfamily\textbackslash verbtocs} \label{verbtocs} -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 +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|^| and \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}. \argu{name} thus contains char with catcodes 12 or 10 for space.\medskip +\argu{name} is the name of the control sequence receiving, with an \verb|\edef|, the \argu{characters}. \argu{name} thus contains tokens with catcodes 12 (or 10 for space).\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 strings containing \guill{|}, you can change at any time the character delimiting the verb content with the macro:\par\medskip +By default, the token delimiting the verb content is \guill{|}. Obviously, this token cannot be both delimiting and being contained into what it delimits. If you need to verbatimize strings containing \guill{|}, you can change at any time the token 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 +Any \argu{token} can be used\footnote{Several tokens 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 token.}. 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} @@ -1098,12 +1321,13 @@ In the following teaching example\footnote{It is possible to make much more simp \subsection{The macro \ttfamily\textbackslash StrRemoveBraces} Advanced users may need to remove the braces of an argument. + The macro \verb|\StrRemoveBraces| does this. Its syntax is:\smallskip -\verb|\StrRemoveBraces|\ARGU{stringe}\arguC{name} +\verb|\StrRemoveBraces|\ARGU{string}\arguC{name} \smallskip -This macro is sensitive to exploration mode and will remove \emph{all} the braces with \verb|\exploregroups| while it will remove braces of higher level with \verb|\noexploregroups|.\medskip +This macro is sensitive to exploration mode and will remove \emph{all} the braces with \verb|\exploregroups| while it will remove braces of lower level with \verb|\noexploregroups|.\medskip \exemple|\noexploregroups| \exemple|\StrRemoveBraces{a{b{c}d}e{f}g}[\mycs]| @@ -1111,7 +1335,7 @@ This macro is sensitive to exploration mode and will remove \emph{all} the brace \exemple|\exploregroups| \exemple|\StrRemoveBraces{a{b{c}d}e{f}g}[\mycs]| \exemple|\meaning\mycs| - +\noexploregroups \subsection{Examples} \label{exemples} Here are some very simple examples involving the macros of this package in programming purposes. @@ -1150,8 +1374,66 @@ With this code, in both arguments, the first token \verb|\empty| is expanded to By this way, \verb|\empty| is a way to "hack" \verb|\expandarg|: it allows to avoid the expansion of all the other tokens. The control sequence \verb|\noexpand| can be used instead of \verb|\empty| for the same result. -\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 +\subsubsection{Exemple 2} +Here, we try to write a macro which gobbles \verb|n| \USs in a string from a given position, and assigns the result to a control sequence. + +Let's call this macro \verb|StringDel| and let's give it this syntax: + +\hfil\verb|\StringDel{string}{position}{n}{\name_of_result}|\hfil{} +\medskip + +We can proceed like this: take the string before the position and save it. Then remove \verb|n + position| \USs from the initial string, and add (concatenate) this result to the string previously saved. This gives the following code:\medskip + +\begin{minipage}[r]{0.7\linewidth} + \hfill + \begin{boxedverbatim} + \newcommand\StringDel[4]{% + \begingroup + \expandarg% local inside this group + \StrLeft{\empty#1}{\number\numexpr#2-1}[#4]% + \StrGobbleLeft{\empty#1}{\number\numexpr#2+#3-1}[\StrA]% + \expandafter\expandafter\expandafter\endgroup + \expandafter\expandafter\expandafter\def + \expandafter\expandafter\expandafter#4% + \expandafter\expandafter\expandafter{\expandafter#4\StrA}% + } + + \noexploregroups + \StringDel{abcdefgh}{2}{3}{\cmd} + \meaning\cmd + + \StringDel{a\textbf{1}b\textbf{2c}3d}{3}{4}{\cmd} + \meaning\cmd + \end{boxedverbatim} + \hspace*{0.3cm} +\end{minipage}% +\begin{minipage}[r]{0.3\linewidth} + \newcommand\StringDel[4]{% + \begingroup + \expandarg% local inside this group + \StrLeft{\empty#1}{\number\numexpr#2-1}[#4]% + \StrGobbleLeft{\empty#1}{\number\numexpr#2+#3-1}[\StrA]% + \expandafter\expandafter\expandafter\endgroup + \expandafter\expandafter\expandafter\def + \expandafter\expandafter\expandafter#4% + \expandafter\expandafter\expandafter{\expandafter#4\StrA}% + } + + \noexploregroups + \StringDel{abcdefgh}{2}{3}{\cmd} + \meaning\cmd + + \StringDel{a\textbf{1}b\textbf{2c}3d}{3}{4}{\cmd} + \meaning\cmd +\end{minipage}\fullexpandarg +\bigskip + +To concatenate, the \LaTeX{} macro \verb|\g@addto@macro| could have been used, leading to a lighter code without the huge bridge of \verb|\expandafter|. The assignment\footnote{The macro {\ttfamily\string\g@addto@macro} can be used if the catcode of "{\ttfamily\string@}" is temporarily changed with {\ttfamily\string\makeatletter} and restored with {\ttfamily\string\makeatother}} can be written like this:\smallskip + +\hfil\verb|\expandafter\g@addto@macro\expandafter#4\expandafter{\StrA}\endgroup|\hfil{} + +\subsubsection{Example 3} +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 First of all, let's cancel the expansion of arguments with \verb|\noexpandarg|, we do not need expansion here. Then, it's easy to cut what is before and behind the first occurrence of \guill{/} (assumed there is a single occurrence) and assign it to \verb|\num| and \verb|\den| and simply call the macro \verb|\frac| :\medskip @@ -1183,7 +1465,7 @@ First of all, let's cancel the expansion of arguments with \verb|\noexpandarg|, \tofrac{x+\sqrt{x}/\sqrt{x^2+x+1}} \end{minipage}\fullexpandarg -\subsubsection{Example 3} +\subsubsection{Example 4} Let's try to write a macro \verb|\boldafter| which writes in bold the first word that follows the word contained in the expansion of \verb|\word|.\medskip \begin{minipage}[r]{0.6\linewidth} @@ -1221,7 +1503,7 @@ Let's try to write a macro \verb|\boldafter| which writes in bold the first word \boldafter{The xstring package is new}{is} \end{minipage}\fullexpandarg -\subsubsection{Example 4} +\subsubsection{Example 5} A control sequence \verb|\myCS| défined with an \verb|\def| contains control sequences with their possible arguments. How to reverse the order of the 2 first control sequences? For this, a macro \verb|\swaptwofirst| does the job and displays the result. But this time, it is not possible to seek the token \verb|\| (catcode 0) with the macros of \Xstring. This is why the use of \verb|\scancs| is necessary: after the detokenization of the argument, it becomes possible to search the char \verb|\| (catcode 12). After 4 lines, the process made by the macros of \Xstring (\verb|\StrBefore| and \verb|\StrBehind|) is finish, a retokenization is done by \verb|\tokenize| and \verb|\before| and \verb|\after| are swapped at this moment.\medskip \begin{minipage}[r]{0.6\linewidth} @@ -1261,102 +1543,41 @@ A control sequence \verb|\myCS| défined with an \verb|\def| contains control se \swaptwofirst{\Large\underline{A}\textbf{B}123} \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 +\subsubsection{Example 6} +In a string, we want to find the $n$\th{} word between 2 given delimiters. For this, let's write a macro \verb|\findword| with an optional argument which content is the delimiter (space by default), 1 argument containing the string and an other argument containing the number $n$.\medskip + +The macro \verb|\findword| artfully uses \verb|\StrBetween| and \verb|\numexpr|:\medskip \begin{minipage}[r]{0.6\linewidth} \hfill \begin{boxedverbatim} - \fullexpandarg - \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 - \expandafter\expandafter\expandafter - \findthegroup% do it again - \fi - \else% unbalanced braces ? - % look for next "}" - \expandafter - \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} + \newcommand\findword[3][ ]{% + \StrBetween[#3,\numexpr#3+1]{#1#2#1}{#1}{#1}} + \noexpandarg + |\findword{a bc d\textit{e f} gh}{3}| + + |\findword[\@nil]{1 \@nil 2 3 \@nil4\@nil5}{2}| \end{boxedverbatim} \hspace*{0.3cm} \end{minipage}% \begin{minipage}[r]{0.3\linewidth} - \fullexpandarg - \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 - \expandafter\expandafter\expandafter - \findthegroup\expandafter% do it again - \fi - \else% unbalanced braces ? - % look for next "}" - \expandafter - \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{minipage} -\medskip + \newcommand\findword[3][ ]{% + \StrBetween[#3,\numexpr#3+1]{#1#2#1}{#1}{#1}} + \noexpandarg + |\findword{a bc d\textit{e f} gh}{3}| -Notice that 2 counters, 2 tests and a double recursion are necessary to find the group: one of each to find which "\verb|}|" delimits the end of the current group, and the others to calculate the number of the group being read.\bigskip\bigskip + |\findword[\@nil]{1 \@nil 2 3 \@nil4\@nil5}{2}| +\end{minipage} +\bigskip\bigskip \begin{center} $\star$\par $\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 +That's all, I hope you will find this package useful!\par +Please, send me an \href{mailto:unbonpetit@gmail.com}{email} if you find a bug or if you have any idea of improvement\ldots\medskip Christian Tellechea \end{document}
\ No newline at end of file |