1

I use LaTeX for text processing in conjunction with a template parser.

I want to do things, when there would be text to print. So I have

\newcommand{\zahltext}{%
  %% here may be dragons, this is generated
}
\ifdefempty{\zahltext}{}{%else
  \subsection{heading}
  \zahltext
}

My problem is, that "%% here be dragons" may be feed with ordinary text, but also with only comments, spaces and newlines. So \zahltext is not empty in the sense of "no characters", but in the sense of "nothing to output".

My question: is there a macro I did not find, that tells me "if LaTeX outputs \zahltext, there would be no output at all or just a paragraph-separator"?

EDITH:

\documentclass[12pt]{article}
\usepackage{etoolbox}
\begin{document}
\newcommand{\zahltext}{}

\renewcommand{\zahltext}{% from Generator
  %% here may be dragons, this is generated
}
\section{should be empty}
\ifdefempty{\zahltext}{cool}{%else
  \subsection{do not print}
  \zahltext
}

\renewcommand{\zahltext}{% from Generator
  content
}
\section{should show}
\ifdefempty{\zahltext}{}{%else
  \subsection{found test}
  \zahltext
}

\renewcommand{\zahltext}{% from Generator

%% an empty line, could not help with that
%% it comes from an extern source

}
\section{should be empty!!}
\ifdefempty{\zahltext}{}{%else
  \subsection{but is not. No output whatsoever}
  \zahltext
}

\end{document}

my problem is with the third case. I want \zahltext to evaluate to "empty", because it creates no visible output.

  • Please provide a full minimal example instead of a sniplet. This may seem tedious to you, but it makes it a lot faster for others to test and help. Besides, not everyone might know where \ifdefempty comes from. Also please make the MWE full, I don't quite understand your last sentence, provide more examples – daleif Jul 12 '18 at 11:07

1 Answers1

1

A very rough solution: you can put the content of \zahltext into a \hbox and check the width of the resulting box. If it's 0, then nothing is typeset.

\documentclass[12pt]{article}

\newcommand{\ifzahltextempty}[2]{%
    \setbox0=\hbox{\zahltext}%
    \ifdim\wd0=0pt #1\else #2\fi
}

\begin{document}
\newcommand{\zahltext}{}

\renewcommand{\zahltext}{% from Generator
  %% here may be dragons, this is generated
}
\section{should be empty}
\ifzahltextempty{cool}{%else
  \subsection{do not print}
  \zahltext
}

\renewcommand{\zahltext}{% from Generator
  content
}
\section{should show}
\ifzahltextempty{}{%else
  \subsection{found test}
  \zahltext
}

\renewcommand{\zahltext}{% from Generator

%% an empty line, could not help with that
%% it comes from an extern source

}
\section{should be empty!!}
\ifzahltextempty{}{%else
  \subsection{but is not. No output whatsoever}
  \zahltext
}

\end{document}

Empty lines are equivalent to \par, which is ignored in an \hbox (that's why this works). As long as only text is in \zahltext, this should give no problem, but it might break down otherwise.

campa
  • 31,130