I would like to define two switches such that this code
\begin{document} \A This is text A Some more text A \B some text B \A again some text A \end{document} produces the following output:
This is text A Some more text A again some text A if switch A is active and B is not,
some text B if switch B is active and A is not, and
This is text A Some more text A some text B again some text A if both are active. I'd like the syntax in the document to be exactly as described, in particular without braces {} and without extra commands.
Edit: Comments suggested I give MWE instead of this vague description of my ideas. Here they are: (all should output A1 A2)
- Using \defs and arguments. Does not work (I believe) because first \A eats everything until last \Z, so \B has no \Z.
\documentclass{article} \def\Z{} \long\def\A#1\Z{\Z#1} \long\def\B#1\Z{\Z} \begin{document} \A A1 \B B1 \A A2 \B B2 \Z %would be hidden in a new def of \end{document} \end{document} - Using \ifs. Does not work (I believe) because the iffalse in \B eats everything until last \fi, and the \fi in \A is not expanded.
\documentclass{article} \def\A{\fi\iftrue} \def\B{\fi\iffalse} \begin{document} \iftrue %would be hidden in a new def of \begin{document} \A A1 \B B1 \A A2 \B B2 \fi %would be hidden in a new def of \end{document} \end{document} - Using \NewDocumentEnvironment. Does not work, but I'm not sure why. The first \A seems to expand fine, but the first \B seems to eat everything until last \end{myenv}.
\documentclass{article} \NewDocumentEnvironment{myenv}{+b}{#1}{} \newcommand{\A}{\end{myenv}\RenewDocumentEnvironment{myenv}{+b}{##1}{}\begin{myenv}} \newcommand{\B}{\end{myenv}\RenewDocumentEnvironment{myenv}{+b}{}{}\begin{myenv}} \begin{document} \begin{myenv} %would be hidden in a new def of \begin{document} \A A1 \B B1 \A A2 \B B2 \end{myenv} %would be hidden in a new def of \end{document} \end{document} OLD: Here were my ideas so far:
- Use conditionals: Set \A to \iftrue and \B to \iffalse in the preamble. Something like
\def\A{\fi\iftrue} \def\B{\fi\iffalse}. I can then redefine the document env to start with \iftrue and end with \fi. The problem is: the \iffalse skips over every token until \fi, even if it expands to \fi... And I don't want to have to type something like \fi\B! - Define \A and \B as commands that take as argument everything until the next \A or \B (Something along the lines of
\def\Z{} \def\A#1\Z{\Z#1} \def\B#1\Z{\Z#1}) I can then redefine one or the other to output #1 or nothing, if I want text A or not. The problem is (I can't seem to get it to scan past \par without braces...) Edit: this works now (see comments). The problem is that the token \Z coming from \B is not expanded, and never seen by the argument scanning of \A... I'm not really as fluent in TeX as I'd like to be, so perhaps I missed something obvious. I welcome any alternative!
\par” Try\long\definstead of\def.\ifactive Aand\ifactive B, or if you prefer\ifactive1and\ifactive2.\if, it scans until it finds the matching\fiwithout expanding macros in between, as I understand it. so you cannot hide the\fiin the definition of a macro unless you ensure that macro is expanded first.\Awithout the terminatingBor whatever, you will get no error until the end of document. and you will get no help from your latex editor in terms of highlighting argument deliminaters etc. for short pieces of text, it is not so bad because you get an error at the next\par, but for long ones it is a pain even when the\fis aren't obfuscated like this.