4

In the MWE below, I use \def\path{numworks/\int_use:N \l__test_this_step_int.png} and \includegraphics[scale = #3]{\path}.

Can I avoid the use \def for a "pure" LaTeX3 coding style?

Note: the next step of my code will be to replace ? by the actual step counter in the argument numworks/?.png given by the user.

\documentclass[a4paper, 12pt]{article}

\usepackage{graphicx}
\usepackage{multicol}

\usepackage{expl3}
\usepackage{xparse}


\ExplSyntaxOn

\int_new:N \l__test_max_step_int
\int_new:N \l__test_this_step_int

\DeclareDocumentCommand\screensteps{vvv}{
    \int_set:Nn \l__test_max_step_int{#1}
    \int_set:Nn \l__test_this_step_int{0}
    
% Lets' print the material...
    \int_do_while:nn {\l__test_this_step_int < \l__test_max_step_int} {
        \int_gincr:N \l__test_this_step_int
        
        \int_if_odd:nTF { \l__test_this_step_int } {
            \begin{multicols}{2}
                \centering
        }{
                \columnbreak
        }
            
                \par\emph{Étape \ \int_use:N \l__test_this_step_int.}
        
                \par\smallskip
                
                %%%%%%%%%%%%     HERE
                \def\path{numworks/\int_use:N \l__test_this_step_int.png}
                %%%%%%%%%%%%     HERE
                \includegraphics[scale = #3]{\path}

        \int_if_even:nT{ \l__test_this_step_int } {
            \end{multicols} 
        }
    }
}

\ExplSyntaxOff


\begin{document}

\screensteps{6}{numworks/?.png}{.6}

\end{document}
5

\def is very generic, so it depends a lot on what you want to do. If you want \def as in “define a command”, then you want \cs_new(_protected):Npn. If you want \def as in “store some data”, then you want \tl_set:Nn. Here you want to store a path to an image, so definitely the latter. Besides, a path to an image can be thought more specifically as a string, so you can use \str_set:Nn.

In the code below I used \str_set:Nx so that any user macros in the argument are expanded, and already added \str_replace_all:Nnn to replace all ? by the current step number. Since you just want to unconditionally loop from 1 to #1, you can use \int_step_inline:nn rather than compare the step number and increase the counter each time. If you use \int_step_inline:nn, then you have the current loop index available as ##1 in the inline code.

The example below tries to include numworks/1.png though numworks/6.png (and fails because I don't have those files :)

\documentclass[a4paper, 12pt]{article}
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn
\str_new:N \l__test_path_str
\DeclareDocumentCommand \screensteps {mmm}
  {
    \int_step_inline:nn {#1}
      {
        \int_if_odd:nTF {##1}
          {
            \begin{multicols}{2}
              \centering
          }
          { \columnbreak }
        \par \emph {Étape ~ ##1 . }
        \par \smallskip
        \str_set:Nx \l__test_path_str {#2}
        \str_replace_all:Nnn \l__test_path_str { ? } {##1}
        \includegraphics[scale = #3]{\l__test_path_str}
        \int_if_even:nT {##1}
          { \end{multicols} }
      }
  }
\ExplSyntaxOff

\begin{document}
\screensteps{6}{numworks/?.png}{.6}
\end{document}

Screenshot added by projetmbc

enter image description here

3
  • 1
    Thanks a lot for this shorter and easier-to-read code. I am thinking of the time needed to do the same thing in pure TeX even if I have definitely stop to use this language.
    – projetmbc
    May 1 at 19:35
  • 1
    @projetmbc Things like loops are actually rather easy to write in pure TeX, but things like \str_replace_all:Nnn are really a pain to reimplement. Also note that in the code I replaced the v arguments by m because you are not really using verbatim there. May 1 at 19:39
  • Yes you're right.
    – projetmbc
    May 1 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.