0

I have a macro that stores some strings in \step0, \step1, \step2... I use the character "a" as a placeholder to be replaced down the line with \ = or \ > down the line (for use in the \tabbing environment). I was hoping t use the \StrSubstitute macro part of the xstring package to do the actual substitution but, obviously, I'm doing something wrong. It seems that the \textbackslash I'm using is not recognised as a \ (wrong catcode?). How can I fix that?

Here is a piece of code to show what I'm expecting and what I actually get.

\documentclass[12pt, a4paper]{article}
\usepackage{xstring}
\usepackage{pgf,tikz}
\usepackage{mathrsfs}
\usepackage{ifthen}

\begin{document}
\large

%%%%%% what It should do...
\begin{tabbing}
G\= = 9 \= - 2 \= × 3 \\
G\> = 9 \> - \>6 \\
G\> = \>1
\end{tabbing}

%%%%%%% what it does    
\expandafter\xdef\csname step0\endcsname{G a= 9 a- 2 a× 3} %define \step0
\expandafter\xdef\csname step1\endcsname{G a= 9 a-  a6} %define \step1
\expandafter\xdef\csname step2\endcsname{G a= aa1 } %define \step2

\def\textbuffer{
\StrSubstitute{dummy}{dummy}{\csname step0\endcsname}[\buffer] % puts \step1 in \buffer
\StrSubstitute{\buffer}{a}{\textbackslash=} \textbackslash\textbackslash %replaces "a" by \= and writes line 0

\foreach \k in {1,...,2}{
\StrSubstitute{dummy}{dummy}{\csname step\k\endcsname}[\buffer] % puts \step\k in \buffer
\StrSubstitute{\buffer}{a}{\textbackslash>}
 \ifthenelse{\k=2}{}{\textbackslash\textbackslash} %replaces "a" by \= and writes line k
 
}
}

\begin{tabbing}
\textbuffer % tries to use \textbuffer as argument to \tabbing
\end{tabbing}
 
\end{document}
1
  • You shouldn't use \k as the running index of your code as \k is an accent macro in LaTeX and already defined. Instead use \tmp or something more unlikely to clash (or, if it has to be a single letter macro, use \x or something else not already defined).
    – Skillmon
    Commented Sep 26, 2022 at 18:08

1 Answer 1

1

The following uses \tl_replace_all:Nnn from the expl3-programming layer instead of xstring functions. I've also changed your input syntax to not use multiple auxiliary macros with numeric names, but a single argument containing each line as a braced group.

\documentclass[12pt, a4paper]{article}

\ExplSyntaxOn
\tl_new:N \l__funkiephil_tmp_tl
\NewDocumentCommand \funkietab { O{a} m }
  {
    \funkiephil_replace:nnx {#1} \= { \tl_head:n {#2} }
    \tl_map_tokens:en { \tl_tail:n {#2} } { \\ \funkiephil_replace:nnn {#1} \> }
  }
\cs_generate_variant:Nn \tl_map_tokens:nn { e }
\cs_new_protected:Npn \funkiephil_replace:nnn #1#2#3
  {
    \tl_set:Nn \l__funkiephil_tmp_tl {#3}
    \tl_replace_all:Nnn \l__funkiephil_tmp_tl {#1} {#2}
    \l__funkiephil_tmp_tl
  }
\cs_generate_variant:Nn \funkiephil_replace:nnn { nnx }
\ExplSyntaxOff

\begin{document}

%%%%%% what It should do...
\begin{tabbing}
G\= = 9 \= - 2 \= × 3 \\
G\> = 9 \> - \>6 \\
G\> = \>1
\end{tabbing}

\begin{tabbing}
  \funkietab {
    {G a= 9 a- 2 a× 3}
    {G a= 9 a-  a6}
    {G a= aa1}
  }
\end{tabbing}
 
\end{document}

enter image description here


The following is an alternative that uses the step<num> notation (in fact it supports arbitrary naming schemes as long as they are numeric). The new macro takes an optional argument giving the character (sequence) that should be replaced by either \= or \> (defaulting to a), a mandatory argument specifying the naming scheme of the variables (using #1 for the numeric part), an optional argument giving the first index (defaulting to 0), and a mandatory argument specifying the last.

\documentclass[12pt, a4paper]{article}

\ExplSyntaxOn
\tl_new:N \l__funkiephil_tmp_tl
\cs_new:Npn \__funkietab_name:n #1 {}
\NewDocumentCommand \funkietab { O{a} m O{0} m }
  {
    \cs_gset:Npn \__funkietab_name:n ##1 {#2}
    \funkiephil_replace:nnn {#1} \= {#3}
    \int_step_inline:nnn { #3 + 1 } {#4}
      { \\ \funkiephil_replace:nnn {#1} \> {##1} }
  }
\cs_new_protected:Npn \funkiephil_replace:nnn #1#2#3
  {
    \tl_set_eq:Nc \l__funkiephil_tmp_tl { \__funkietab_name:n {#3} }
    \tl_replace_all:Nnn \l__funkiephil_tmp_tl {#1} {#2}
    \l__funkiephil_tmp_tl
  }
\ExplSyntaxOff

\begin{document}

%%%%%% what It should do...
\begin{tabbing}
G\= = 9 \= - 2 \= × 3 \\
G\> = 9 \> - \>6 \\
G\> = \>1
\end{tabbing}

\expandafter\xdef\csname step0\endcsname{G a= 9 a- 2 a× 3} %define \step0
\expandafter\xdef\csname step1\endcsname{G a= 9 a-  a6} %define \step1
\expandafter\xdef\csname step2\endcsname{G a= aa1 } %define \step2

\begin{tabbing}
  \funkietab{step#1}{2}
\end{tabbing}
 
\end{document}

Output like above.

6
  • thanks a lot. I know nothing about expl13, I guess I'm going to have to look it up.
    – FunkiePhil
    Commented Sep 26, 2022 at 19:32
  • @Skilmon It works the way you wrote it but I need to have the numbered macros as input. I tried to replace each line with something like \csname step0\endcsname but it doesn't work.
    – FunkiePhil
    Commented Sep 26, 2022 at 19:44
  • @FunkiePhil most likely you don't need the numbered macros... But I'll make some changes.
    – Skillmon
    Commented Sep 26, 2022 at 20:02
  • @FunkiePhil does the second code work for you?
    – Skillmon
    Commented Sep 27, 2022 at 15:49
  • @Skilmon Yes it does! Thanks a lot, now I've got to understand how it works.
    – FunkiePhil
    Commented Sep 27, 2022 at 17:42

You must log in to answer this question.

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