# Is there a way to load only previously tagged environments?

This is a bit complicated, so let me explain what I wondered if it'd be an actual thing.

I have four .tex files named main.tex, tex1.tex, tex2.tex and tex3.tex.

Now, I'll put a simplified version of my actual files, here they are.

First one:

%this is 'tex1'.

\mainenvironment{\mycommand{tag1}{tag2}
Q1 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q2 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q3 This is a text environment. Here, I put some texts, images, other environments, etc.
}


Next one:

%this is 'tex2'.

\mainenvironment{\mycommand{tag2}{tag3}
Q4 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag3}
Q5 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q6 This is a text environment. Here, I put some texts, images, other environments, etc.
}


And the last one:

%this is 'tex3'.

\mainenvironment{\mycommand{tag1}{tag2}
Q7 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag3}
Q8 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag2}
Q9 This is a text environment. Here, I put some texts, images, other environments, etc.
}


Now, for my main.tex, here is how it goes:

\documentclass{article}
\newcommand{\mainenvironment}[1]{#1}
\usepackage{import}

\begin{document}

\import{tex/}{tex1.tex}

\end{document}


As you can see, I have an empty command called \mainenvironment, which I want there for later flexibility (if I want to change the before/after each text block). But that's not the problem. Here's the thing I'm wondering if it's possible.

I'd like to know if one can load only some of the tags I gave with that \mycommand command. For example, after the \begin{document}, I'd call some command in order to load only tag1 from those three files I've created. And then, it'd compile only the text blocks with Q1, Q5, Q7, Q8 and Q9.

I thought that maybe one could change \mainenvironment command so it'd be "taggable" and then using something along \import to load only tagged textblocks (which are the texts inside the \mainenvironment commands), but I couldn't solve this specific puzzle.

Can anyone help?

• You're only talking about a single tag, yet \mycommand takes two arguments, both of which seem to be tags. Does your tagging requirement hold for both arguments of \mycommand? Also, is there a specific need to use import? Why not just \input? – Werner May 4 at 19:24
• ctan.org/pkg/tagging?lang=en may be what you are looking for. The package only allows activating "tag1 OR tag2", not "tag1 AND tag2". If you need the latter, you can use the modified version that I wrote sometime ago. – Willie Wong May 4 at 19:41

The following setup does what you need:

\begin{filecontents*}[overwrite]{tex1.tex}
%this is 'tex1'.

\mainenvironment{\mycommand{tag1}{tag2}
Q1 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q2 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q3 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}

\begin{filecontents*}[overwrite]{tex2.tex}
%this is 'tex2'.

\mainenvironment{\mycommand{tag2}{tag3}
Q4 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag3}
Q5 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag2}{tag3}
Q6 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}

\begin{filecontents*}[overwrite]{tex3.tex}
%this is 'tex3'.

\mainenvironment{\mycommand{tag1}{tag2}
Q7 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag3}
Q8 This is a text environment. Here, I put some texts, images, other environments, etc.
}

\mainenvironment{\mycommand{tag1}{tag2}
Q9 This is a text environment. Here, I put some texts, images, other environments, etc.
}
\end{filecontents*}

\documentclass{article}

\makeatletter
\newcommand{\approvedtag}[1]{\def\@approvedtag{#1}}% Store approved tag

\newcommand{\mycommand}[2]{%
% Assumed \mycommand{<tagA>}{<tagB>} is at the beginning of \mainenvironment
\approvedfalse% Default is to not print anything
\ifnum\pdfstrcmp{#1}{\@approvedtag}=0
\approvedtrue% Approved tag in argument #1
\else
\ifnum\pdfstrcmp{#2}{\@approvedtag}=0
\approvedtrue% Approved tag in argument #2
\fi
\fi
\ignorespaces
\ifapproved% Conditionally print remainder of \mainenvironment
}
\newif\ifapproved
\newcommand{\mainenvironment}[1]{%
#1% Process main environment
\fi% Close condition
}
\makeatother

\begin{document}

\approvedtag{tag1}

\input{tex1}

\input{tex2}

\input{tex3}

\end{document}


\approvedtag{<tag>} allows you to specify the tag you want to be considered as approved. The solution updates \mycommand{<tagA>}{<tagB>} to test whether the approved <tag> matches either <tagA> or <tagB> and then prints the remainder of \mainenvironment. Inherently this assumes that content preceding \mycommand will always be printed and should therefore be the first element within \mainenvironment.