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 sequenceis 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}
\Subentryat 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. – David Carlisle Oct 09 '17 at 06:59\Subentryinto your environment, presumably you want to use it don't you???? – David Carlisle Oct 09 '17 at 11:52##1##2not#1#2but also it should be used inside the environment where it is defined not outside. – David Carlisle Oct 09 '17 at 11:54\Subentrybefore 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. – wipet Oct 09 '17 at 17:58\SubentryBdefinition. This was mentioned by David Carlisle earlier. You must write##1##2in\SubnetryBdefinition-mask and##1and##2in\SubentryBdefinition-body. All another errors are only a consequence of this first error. – wipet Oct 09 '17 at 19:08\SunbentryBis internal macro and it must be defined only with internal parameters##1and##2. If you need something to do with the external parameters#1and#2, then do such things outside the\SubentryBdefinition (or only in its definition-body). I repeat: I don't know your intend so, I cannot help you. I only see your deep misunderstanding of TeX behaviour and your struggle to hide your real intend. For example, we don't know how you want to define\Subentryexplicitly inside the environment. This brings only new problems for you. – wipet Oct 10 '17 at 06:29