A Command, e.g. \Subentry
, works fine when defined outside a LaTeX3 Environment, e.g. {mainentry}
, but as soon as one would like to put its definition inside the (definition of the) mentioned Environment a Undefined control sequence-error for that mentioned Command is yielded, meaning somehow LaTeX can not read its definition.
In MWE 1, no error is produced, apparently because the lines containing the \Subentry command, namely the following lines:
\def\Subentry{\def\SubentryS{\def\SubentryS{ \endgraf }}\futurelet\next\SubentryA}
\def\SubentryA{\ifx\bgroup\next\expandafter\SubentryB\fi}
\def\SubentryB#1#2{\SubentryS { #1 #2 {\bfseries #1} } \futurelet\next\SubentryA}
are defined outside of the Environment {mainentry}
.
- In MWE 1, no
Undefined control sequence
is produced. - In MWE 2, those lines will be placed inside the Environment
{mainentry}
, at the place of% ???
inside MWE 1. We also put the issuing of the Command, namely\Subentry{C}{D}
within the issuing of the Environment. Then, we shift the definitions of the Command to the beginning of the Environment, instead of putting them at the end (following User Wipet's comment).
MWE 1 (no error)
% !TEX TS-program = lualatexmk
\documentclass[a4paper]{scrreprt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Declare packages and commands
%
\usepackage{xparse}
\ExplSyntaxOn
\cs_set_eq:NN
\IfEmptyTF
\tl_if_blank:nTF
\ExplSyntaxOff
%
\NewDocumentEnvironment {mainentry} { m m m } {%
% ???
\IfEmptyTF{#3}{%
#1 #2
}{%
#1 #2 \endgraf #3
}
}{%
}
%
%
%
\def\Subentry{\def\SubentryS{\def\SubentryS{ \endgraf }}\futurelet\next\SubentryA}
\def\SubentryA{\ifx\bgroup\next\expandafter\SubentryB\fi}
\def\SubentryB#1#2{\SubentryS { #1 #2 {\bfseries #1} } \futurelet\next\SubentryA}
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{mainentry}{A}{B}{}
\end{mainentry}
Some text.
\Subentry{C}{D}
\end{document}
MWE 2 (error)
Please note that I would like to put the (definition of the) Command \Subentry
inside the (definition of the) Environment {mainentry}
because I would like {\textbf #1}
(which occurs inside that Command), to be replaced with {\textbf ##1}
(or whatever code necessary ... I don't know exactly what syntax to use) so that it actually takes in the first argument of the Environment (so then {mainentry}
) where it is placed in (speaking about definitions of), not just the Commands' own first argument.
Currently, the instance of {\textbf #1}
yields C
(in casu, the first argument of the Command it finds itself in), whereas I would like it to yield A
(the first argument of the encompassing Environment, one level up in hierarchy, in which that Command may find itself) (speaking about definitions of).
Therefor, MWE 2 uses {\textbf ##1}
instead of {\textbf #1}
. (following User David Carlisle's comment). ---> PLEASE NOTE THAT I MISINTERPRETED THIS COMMENT ... NOTE BY OP AFTER FURTHER UNDERSTANDING ... ##1 actually refers to the inner argument, not the outer argument <---
The error will look something as follows:
./test.tex:69: Use of \SubentryB doesn't match its definition.
% !TEX TS-program = lualatexmk
\documentclass[a4paper]{scrreprt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Declare packages and commands
%
\usepackage{xparse}
\ExplSyntaxOn
\cs_set_eq:NN
\IfEmptyTF
\tl_if_blank:nTF
\ExplSyntaxOff
%
\NewDocumentEnvironment {mainentry} { m m m } {%
\def\Subentry{\def\SubentryS{\def\SubentryS{ \endgraf }}\futurelet\next\SubentryA}
\def\SubentryA{\ifx\bgroup\next\expandafter\SubentryB\fi}
\def\SubentryB#1#2##1{\SubentryS { #1 #2 {\bfseries ##1} } \futurelet\next\SubentryA}
\IfEmptyTF{#3}{%
#1 #2
}{%
#1 #2 \endgraf #3
}
}{%
}
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{mainentry}{A}{B}{}
\Subentry{C}{D}
\end{mainentry}
Some text.
\end{document}
\Subentry
at the place you have???
I get no error, but you don't say what you did, what error you got or what output you expected so it is hard to help.\Subentry
into your environment, presumably you want to use it don't you????##1##2
not#1#2
but also it should be used inside the environment where it is defined not outside.\Subentry
before it is defined. I don't have another MWE which shows other errors. If I put the definitions into the starting paramater immediately before\IfEmptyTF{#3}
then there is no errors. Of course, you must to double all hash marks in definitions as mentioned by David Carlisle.