1

I am using conditional compilation to test the speed of compiling a huge book. This is the way I do it within a \begin{figure} environment:

\ifforceprecompiled
  \includegraphics{timeline.tex.pdf}
\else
  \ifstandalonemode
    \includetikz{timeline.tex}
  \else
    \includetikz{timeline.tikz}
  \fi
\fi

The problem with this approach is that if the figure was created with the timeline.tikz file, then the compiled figure is timeline.tikz.pdf and not timeline.tex.pdf, so I can't use the ifforceprecompiled macro in that case. I would like to do instead:

\ifforceprecompiled
  \includegraphics{timeline.pdf}.  % <- change here
\else
  \ifstandalonemode
    \includetikz{timeline.tex}
  \else
    \includetikz{timeline.tikz}
  \fi
\fi

But for that I need to somehow get only the base name of the file (without the extension). I was wondering whether something like this could be done with a macro.

UPDATE

Following the advice of David Carlisle, this is what I have so far in the preamble:

\newcommand{\myinclude}[1]{
\ifforceprecompiled
  \includegraphics{#1.pdf}
\else
  \ifstandalonemode
    \tikzsetnextfilename{#1}
    \input{#1.tex}
  \else
    \tikzsetnextfilename{#1}
    \input{#1.tikz}
  \fi
\fi
}

So I just pass the filename without the extension to the macro and everything is taken care of properly. However, one needs to be aware of not passing the extension, otherwise there's an error. I think still figuring out a way to obtain the base name of the file will make this macro more robust.

1
  • 3
    you could use \filename@parse (which is what \includegraphics uses internally) but I would not have all those tests on every instance, just have \myinclude{timeline} in the figure and define that to be \includetikz{#1.tikz} or \includetikz{#1.tex} or \includegraphics{#1.pdf} with a definition in a single conditional block in the preamble Commented Nov 23, 2022 at 10:40

2 Answers 2

1

In case the switches \ifstandalonemode/\ifforceprecompiled are independent from each other and in case of "forceprecompiled"

  • standalone-mode always goes along with using timeline.tex.pdf
  • non-standalone-mode always goes along with using timeline.tikz.pdf

, you can—as suggested by David Carlisle—probably try s.th. like this:

Preamble:

\ifstandalonemode
  \ifforceprecompiled
     \newcommand\myinclude[1]{\includegraphics{#1.tex.pdf}}% or #1.pdf
  \else
     \newcommand\myinclude[1]{\includetikz{#1.tex}}%
  \fi
\else
  \ifforceprecompiled
    \newcommand\myinclude[1]{\includegraphics{#1.tikz.pdf}}% or #1.pdf
  \else
    \newcommand\myinclude[1]{\includetikz{#1.tikz}}%
  \fi
\fi
...

document-environment:

...
\myinclude{timeline}%
...
1

You should be able to use \IfFileExists{<file>}{<true>}{<false>} since there's only two options really.

In the mockup below I have two "pre-compiled" images in the project/source folder:

  • example-image-a.tex.pdf
  • example-image-b.tikz.pdf

These are copies of the similarly-named example-images from mwe, just for the example.

With \forceprecompiledtrue, you'll see that either of the precompiled output (.tex.pdf and .tikz.pdf) is included as needed, depending on the file that is availble for inclusion. You may have to use a similar conditional within the other pathway(s).

enter image description here

\documentclass{article}

\usepackage{graphicx}

\NewDocumentCommand{\includefigure}{ O{} m }{%
  \ifforceprecompiled
    \IfFileExists{#2.tex.pdf}
      {\includegraphics[#1]{#2.tex.pdf}}
      {\includegraphics[#1]{#2.tikz.pdf}}%
  \else
    \ifstandalonemode
      \includetikz{#2.tex}% May need to condition here as well
    \else
      \includetikz{#2.tikz}% May need to condition here as well
    \fi
  \fi
}

\newif\ifforceprecompiled
\newif\ifstandalonemode

\forceprecompiledtrue

\begin{document}

\begin{figure}
  \centering
  \includefigure[height=3em]{example-image-a}
  \caption{Example image~A}

  \bigskip
  
  \includefigure[height=3em]{example-image-b}
  \caption{Example image~B}
\end{figure}

\end{document}

You must log in to answer this question.

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