# Environment which sends text conditionally to an appendix?

I am writing a paper and would like an environment that has the following property. If some flag is set to "true" in the preamble, all text wrapped in that environment is sent to the appendix. If the flag is "false", the text is placed inline. Ideally, it would also be possible to send the text to a specific subsection of the appendix (the specific subsection could be an argument to the environment?). Does something like this already exist? My ideal usage would look something like:

\documentclass{article}
\ifappendix[true]

\begin{document}

This text is always in the main body of the paper.

\begin{maybeappendix}[~\ref{sec:appendix-subsection}]
This text is produced inline if \ifappendix[false], thrown into the subsection ~\ref{sec:appendix-subsection} if \ifappendix[true].
\end{maybeappendix}

\appendix
\section{Section Name}\label{ref:appendix-subsection}
\end{document}


I've tried to define a new command that would do this, that looks like this:

\documentclass{article}
\usepackage{etoolbox}

\newif\iffull
\fulltrue


\begin{document}

\condloc{\where}{Text Should be inserted before app if full is true,
after app if full false.}

\section{app}
\where
\end{document}


which doesn't render anything if full is false, and places the text where I want for false when true, rather than placing the text in the intended places (before app if full is true and after app if full is false).

• I'm pretty sure this can be done with a layer on top of constructions using the answers package. If no one else provides a solution I may work on one tomorrow. – Ethan Bolker Nov 4 '14 at 14:55

This should do the trick.

\documentclass{article}

% Beginning of definitions
\usepackage{environ}

\newif\ifappendix
\NewEnviron{maybeappendix}[1]
{\ifappendix
\expandafter\global\expandafter\let\csname putmaybeappendix#1\endcsname\BODY%
\else
\expandafter\newcommand\csname putmaybeappendix#1\endcsname{}\BODY%
\fi}
\newcommand{\putmaybeappendix}[1]{\csname putmaybeappendix#1\endcsname}
% End of definitions

\appendixtrue

\begin{document}

Main body

\begin{maybeappendix}{label}
Movable text
\end{maybeappendix}

\appendix
\section{Section name}
\putmaybeappendix{label}

\end{document}


Change \appendixtrue to \appendixfalse to place the text where it is (or just delete it, as \appendixfalse is default (to change the default, add \appendixtrue after \newif\ifappendix)).

It seems to support several paragraphs and even figures in the movable text :)

If you want to collect several texts with the same label, use the following definition instead (the code above would discard an old text with that label):

\usepackage{environ,etoolbox}

\newif\ifappendix
\NewEnviron{maybeappendix}[1]
{\ifcsname putmaybeappendix#1\endcsname
\else
\expandafter\gdef\csname putmaybeappendix#1\endcsname{}
\fi
\ifappendix
\expandafter\xdef\csname putmaybeappendix#1\endcsname{\expandafter\expandonce\csname putmaybeappendix#1\endcsname\unexpanded{\par}\expandonce\BODY}
\else
\BODY%
\fi}
\newcommand{\putmaybeappendix}[1]{\csname putmaybeappendix#1\endcsname}


Then all texts are collected and separated with \par in the appendix (if the \par is undesired, just remove \unexpanded{\par} from the definition).

• How would I collect content in the same appendix? I've been having trouble working out how to stick more content on the end of the internal variable, rather than simply overwriting it as this solution would. – figlesquidge May 18 '15 at 17:31
• @figlesquidge see edit :) – Jonas Granholm May 18 '15 at 19:33
• Thanks very much. I think my error was forgetting to check it was defined first before using it in the same iterative manner you do. (Sorry can't give you another upvote for the updated version!) – figlesquidge May 18 '15 at 19:54

So this is a janky workaround, but the following has the intended consequences:

\documentclass{article}
\usepackage{etoolbox}

\newif\iffull
\fullfalse

\newcommand{\condloc}[2]{\newrobustcmd{#1} \protect{\iffull{#2}\else{\gpreto#1{#2}}\fi}}

\begin{document}
\condloc{\where}{Text Should be inserted before app if full is true, after app if full false.}

\section{App}
\where
\end{document}


I'm not sure how to do this with an environment, but it works for simple things, at least.