This is a follow-up on the answers here. I found two cases where the proposed solutions fail, and I am looking for ways to address it. Consider the MWE:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\long\def\ignoreThis#1{%
\begingroup
\setbox0\vbox{{\everypar{}
\begin{minipage}{\linewidth}
#1\end{minipage}}}%
\endgroup}%
\newcounter{mycounter}
\begin{document}
Note that \ignoreThis{this text is not shown but \addtocounter{mycounter}{5}}
side effects are processed: \themycounter
\end{document}
This works well except in two cases that I have identified (I am sure there are more as these seem related to output to aux files).
\ignoreThis{\begin{equation}
\label{eq} A=B \end{equation}
Reference inside \ref{eq}.}
This causes the warning Reference 'eq' on page 1 undefined. I could redefine \ref locally in \ignoreThis, but I doubt this is a good a solution. Another more critical issue is caused by pgfplots:
\ignoreThis{\begin{tikzpicture}\begin{axis}
\addplot[only marks,mark=*]%
table[x=x, y=y] {x y
2 4
4 4
3 3};
\end{axis}\end{tikzpicture}}
I get the error Package pgfplots Error: Could not read table file. I am able to workaround this issue by defining the table outside \ignoreThis and using it in a plot.
Is there a better way to fix these issues?
\includepreserves counters. Yes, that does seem to work. Although,\includeadds a\clearpagebefore and after, but maybe there is a way to disable that. More importantly, would this mean that I have to create (and keep track of) a separate file every time I want to hide part of my document? In my use case, I actually control the inclusion of the content inside my command with a toggle. And I actually use this command tens of times (to hide solutions to questions in an exam paper). – Tohiko Feb 11 '22 at 13:37\ignoreThisand replace them with empty nodes of the same dimensions. Unfortunately I do not have a lot of time recently and I am not good enough in LuaTeX to give you an answer quickly. Maybe you can rephrase your question in LuaTeX-related form and consult with some of the LuaTeX experts here. – Alan Xiang Feb 27 '22 at 17:14