6

I want a mode where the output consists of ONLY a single macro. In the MWE below, that is the output of \ImportantMacro. All other text outside of this macro is to be ignored.

An obvious start to this problem is to redfine the non \ImportantMacros when in this mode:

\ifdefined\OnlyOutputImportantMacro% Redefine all macros to be ignored.
    \renewcommand{\MacroToBeIgnored}[1]{}%
\fi

but, what about text that is not in a macro? The MWE below outputs:

enter image description here

where all the text in red should be suppressed, and only text in blue should be output.

Somewhat Related References:

Bonus:

  • Is there an easier method than redefining all the macros that are not the one macro that I am interested in?
  • Is it possible to ignore a macro in the main document, yet not ignore it if it appears within the \ImportantMacro. Of coure I could do the brute force method of setting a flag when entering \ImportantMacro and then conditionally ignoring the macro unless the flag is set. Or redefining macros when within \ImportantMacro:

    \newcommand{\ImportantMacro}[1]{%
        \ifdefined\OnlyOutputImportantMacro
            \renewcommand{\MacroToBeIgnored}[1]{\begingroup\textcolor{blue}{\bfseries##1}\endgroup}%  BONUS
        \fi
        \textcolor{blue}{#1}%
        \ifdefined\OnlyOutputImportantMacro
            \renewcommand{\MacroToBeIgnored}[1]{}%
        \fi
    }%
    

    is not really a practical option. Wondering if there is a more TeXish way to do this. This is the bold blue text in the output.

Code:

\def\OnlyOutputImportantMacro{}
\documentclass{article}
\usepackage{xcolor}


\newcommand{\MacroToBeIgnored}[1]{\textcolor{red}{#1}}%
\newcommand{\ImportantMacro}[1]{%
    \ifdefined\OnlyOutputImportantMacro
        \renewcommand{\MacroToBeIgnored}[1]{\begingroup\textcolor{blue}{\bfseries##1}\endgroup}%  BONUS
    \fi
    \textcolor{blue}{#1}%
    \ifdefined\OnlyOutputImportantMacro
        \renewcommand{\MacroToBeIgnored}[1]{}%
    \fi
}%

\ifdefined\OnlyOutputImportantMacro% Redefine all macros to be ignored.
    \renewcommand{\MacroToBeIgnored}[1]{}%
\fi

\begin{document}\color{red}
Some text outside of macro that needs to be ignored.
\MacroToBeIgnored{%
    Some text inside of macro that needs to be ignored.%
}
\ImportantMacro{%
    Some text inside of macro that needs to be output.
    \MacroToBeIgnored{Bonus text not to be suppressed.}%
}
\MacroToBeIgnored{%
    Some text inside of macro that needs to be ignored.%
}
Some more text outside of macro that needs to be ignored.
\end{document}
  • You could look at how Beamer implements ignorenonframetext. – cfr Mar 5 '18 at 0:51
6

Proof of concept:

\documentclass{article}

\newcommand\ImportantMacro[1]{%
  \gdef\ImportantMacroArg{#1}%
  \aftergroup\ImportantMacroArg
}
\AtBeginDocument{\setbox0=\vbox\bgroup}
\AtEndDocument{\egroup}

\begin{document}

whatever you want
\ImportantMacro{This will print}
some other text

\end{document}

You're trying to stretch too much, I'm afraid.

  • Hmmm.. This looks promising... – Peter Grill Mar 5 '18 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.