3

In my code some pictures should be displayed if any condition is satisfied.

The conditional must necessarily be placed within the picture environment.

The question is how to insert a line break in the text ONLY if the picture actually took place.

A MWE:

\documentclass{article}
\usepackage{tikz}

\newcommand{\picturexample}[1]{%
\begin{tikzpicture}
  \ifodd#1              % In practice, a PGF/TikZ's check command
    \draw (0,0) circle (1);
  \fi
\end{tikzpicture}}

\newcount\n

\begin{document}

\loop
  \picturexample{\n}%
  \\                    % this should happen only when the picture "exists"
  \advance\n1
\ifnum\n<10\repeat

\end{document}
3
  • This belongs to meta-tikz question ^^
    – Black Mild
    Oct 31, 2021 at 15:18
  • Couldn't you perhaps create a custom \if that is set to true if the picture is drawn? Oct 31, 2021 at 15:46
  • @JasperHabicht, Yes, I considered that possibility, but I don't want to mess up the code so much.
    – e_moro
    Nov 1, 2021 at 8:32

1 Answer 1

5

You can store the picture in a box, measure it, and if it has no content then all its dimensions are 0. This does not work for overlay pictures, though.

\documentclass{article}
\usepackage{tikz}

\newcommand{\picturexample}[1]{%
\begin{tikzpicture}
  \ifodd#1              % In practice, a PGF/TikZ's check command
    \draw (0,0) circle (1);
  \fi
\end{tikzpicture}}

\newcount\n

\begin{document}
\noindent
\loop
  \setbox0\hbox{\picturexample{\n}}%
  \ifdim\dimexpr\wd0+\ht0+\dp0>0pt\relax
    \usebox0\\
  \fi                       % this should happen only when the picture "exists"
  \advance\n1
\ifnum\n<10\repeat
\end{document}
2
  • Measuring the box generated by tikzpicture was the idea I had in mind, but I didn't know how to do it. Perfect solution for me.
    – e_moro
    Nov 1, 2021 at 8:27
  • By the way, I think it is enough to measure one dimension,
    – e_moro
    Nov 1, 2021 at 9:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .