1

I need to dynamically apply an environment (or possibly a series of nested environments)on some text using expl3 (latex3) commands. However, the environment behaves differently when concatenate a series of environment related tokens using expl3, in compare to when I use it normally by embedding the text. Using expl3, any content inside the environment which comes after the passed body content (that is +b), appears at the end of document, instead of appearing next to the passed body content.

What is the problem?

The MWE below shows the problem. Result of MWE

\documentclass{minimal}

\RequirePackage{expl3}

\ExplSyntaxOn

\NewDocumentEnvironment{myenv}{ +b}{
        \tl_set:Nn \l_myenv_body_tl {#1}

    <myenv~begins>~\use:n  \l_myenv_body_tl
    <This~should~appear~before~myenv~ending.>
}{ <myenv~ends~here.>}
 
\ExplSyntaxOff



\begin{document}

    First line.
    
    \vspace \medskipamount

    % An environment by normal approach
    \begin{myenv}
        Some text for the regular body.
    \end{myenv}

    \vspace \medskipamount  

    Second line.

    \vspace \medskipamount
    
    % An environment created using Expl3
    \ExplSyntaxOn

    \tl_set:Nn \l_body_tl {Some~text~for~the~body.}
    \tl_set:Nn \l_beginings_tl {\begin{myenv} }
    \tl_set:Nn \l_endings_tl {\end{myenv}}
    \tl_set:Nn \l_total_tl {}   
    
    \tl_put_right:Nn \l_total_tl {\l_beginings_tl} 
    \tl_put_right:Nn \l_total_tl {\l_body_tl} 
    \tl_put_right:Nn \l_total_tl {\l_endings_tl} 

    \use:n {\l_total_tl}    
 
    \ExplSyntaxOff


    \vspace \medskipamount
    
    Third line.
    
    
\end{document}

4
  • 1
    Use \tl_put_right:NV instead to expand \l_endings_tl.
    – Skillmon
    Apr 15, 2022 at 21:09
  • @DavidCarlisle, I checked again and the image is the original to the code, just with my annotations. I am using Tex live 2022 Lualatex (if this can make a difference).
    – Shahryar
    Apr 16, 2022 at 3:44
  • @Skillmon, Thanks! it helped me in the actual solution.
    – Shahryar
    Apr 16, 2022 at 3:45
  • 1
    Sorry should I have thought of luatex, we usually assume pdftex unless a different engine mntioned:-) Apr 16, 2022 at 9:17

1 Answer 1

2

you are using a b argument so that needs to see a literal \end{myenv} it can not be hidden behind a macro (you would see the same if you used \def\foo{\end{myenv}} the fact you used expl3 not really related to the issue.

Simpler non expl3 example

\documentclass{article}
\usepackage[T1]{fontenc}


\NewDocumentEnvironment{myenv}{ +b}{
    <myenv~begins>
        #1
    <This~should~appear~before~myenv~ending.>
}{ <myenv~ends~here.>}
 

\def\fooA{\begin{myenv}}
\def\fooB{\end{myenv}}



\begin{document}

    First line.

    \begin{myenv}
      abc
    \end{myenv}

    Second line.

\fooA
  xyz
\fooB

    Third line.

\end{document}

enter image description here

1
  • Thanks for clearing that. It also show the solution.
    – Shahryar
    Apr 16, 2022 at 3:42

You must log in to answer this question.

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