1

I'm using package newfloat to define an additional figure environment (for supplementary figures). A journal asks me to provide these in a separate document.

Ideally, I'd just need to set a variable to 0 or 1 in the header and compliation with pdflatex would produce a pdf file, either without the supplementary figures at all or with them at the end of the file. I'd like of course to keep the references to labels of my supplemetary figures working - and I'd like to keep the hyper references working too (except those to supplementary figures of course), so I believe this means avoid splitting the pdf a posteriori (but I'd accept a solution that preserves hyperlinks but involves a PDF splitting step using a linux command).

MWE:

\documentclass{article}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{newfloat}
\DeclareFloatingEnvironment[
    fileext=los,
    listname={List of Supplementary Figures},
    name={Supplementary Figure},
    placement=tbhp,
]{suppfigure}
\usepackage[nomarkers,tablesonly,notablist]{endfloat}
\DeclareDelayedFloatFlavor*{suppfigure}{table}
\begin{document}
There's the main figure \ref{fig.main} and 
the supplemetary one \ref{fig.supp}. 
The main one should be at the bottom of this page, 
the supplementary one in a separate pdf file.
\begin{figure}[b]
\centering\Huge{A}
\caption{Main fig}
\label{fig.main}
\end{figure}
\begin{suppfigure}[b] 
\centering\Huge{B}
\caption{Supp fig}
\label{fig.supp}
\end{suppfigure}
\lipsum[1-7]
\end{document}

(note that I'm declaring Supp Figures as "tables" as far as endfloat is concerned, because it seems that package won't accept an option leaving figures in the text and putting a custom floating figure environment at the end.)

EDIT: I've tried to ask endfloat to embed delayed figures into a "blackhole" environment, but I get an error upon compilation. I've inserted in latex file preamble:

%% Black hole for supplementary figures
\newif\ifshowsupp
\showsupptrue
\usepackage{environ}
\NewEnviron{hidefloat}{
  \ifshowsupp
    \BODY
  \fi}
\renewcommand{\efloatpreamble}{\begin{hidefloat}}
\renewcommand{\efloatseparator}{\end{hidefloat}\begin{hidefloat}}
\renewcommand{\efloatpostamble}{\end{hidefloat}}

Error is "! Argument of \hidefloat has an extra }." and is said to be raised when reaching \end{document}, although I've checked the \efloat... commands are called as claimed by endfloat. I've tried using solution environment from package exsheet and got the same result.

2
  • You could use the package pdfpages to split the pdf afterward. The downside is I don't think it preserves hyperlinks (and since you explicitly mentioned preserving hyperlinks, I'm guessing you already knew about pdfpages).
    – Teepeemm
    Oct 28, 2021 at 18:08
  • @Teepeemm Indeed, pdfpages or pdfjam (which I think wraps pdfpages in a linux command) can do that but won't preserve links.
    – Joce
    Oct 28, 2021 at 21:03

1 Answer 1

1

My approach would be to automatically capture all suppfigure environments into a new file, say, suppfigs.tex. This file will be built progressively during compilation with any suppfigures you may use. Then, \AtEndDocument, you can import suppfigs.tex as-is, setting all the supplemental figures at the end of the document. This preserves all \references and hyperlinking.

enter image description here

\documentclass{article}

\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{newfloat}
\DeclareFloatingEnvironment[
    fileext=los,
    listname={List of Supplementary Figures},
    name={Supplementary Figure},
    placement=tbhp,
]{suppfigure}

\usepackage{newfile}

\newoutputstream{suppfigs}
\AtBeginDocument{%
  \openoutputfile{suppfigs.tex}{suppfigs}% Create file that will store supplemental figures
  \addtostream{suppfigs}{
    % Restore way supplemental figures are handled (originally redefined to only store its contents)
    \protect\let\protect\suppfigure\protect\oldsuppfigure
    \protect\let\protect\endsuppfigure\protect\endoldsuppfigure
  }
}
\AtEndDocument{%
  \closeoutputstream{suppfigs}% Close stream that should contain any supplemental figures
  \input{suppfigs}% Input gathered supplemental figures
}

\usepackage{environ}
% Despite creating supplemental figure floating environment, redefine it to just capture its contents.
% First store the original definitions (of \begin{suppfigure} and \end{suppfigure})
\let\oldsuppfigure\suppfigure
\let\endoldsuppfigure\endsuppfigure
% Redefine suppfigure to write its content to the suppfigs output stream
\RenewEnviron{suppfigure}[1][tbhp]{%
  % Within the environment we temporarily change the definition of fragile elements to \relax.
  % This ensures they won't be expanded when written to the suppfigs.tex output file.
  \let\label\relax
  \let\caption\relax
  % List other commands here that you may use that need protection from expansion (remember, this is only temporary)
  \addtostream{suppfigs}{% Write entire environment to output file
    \begin{suppfigure}[#1]
      \BODY
    \end{suppfigure}
  }%
}

\begin{document}

There's the main Figure~\ref{fig.main} and 
the supplementary one as Figure~\ref{fig.supp}. 
The main one should be at the bottom of this page, 
the supplementary one in a separate PDF file.

\begin{figure}[b]
  \centering\Huge{A}
  \caption{Main fig}
  \label{fig.main}
\end{figure}

\begin{suppfigure}[b] 
  \centering\Huge{B}
  \caption{Supp fig}
  \label{fig.supp}
\end{suppfigure}

\lipsum[1-20]

\end{document}
2
  • Brilliant! This does work very well in my MWE. In the real world of my whole manuscript though, I'm getting errors when \BODY is being processed, ! Undefined control sequence. \BODY ...ength {\cbheight }{.3\halffig } \def \xl(newline here) {.3} \def \xhw {.15} \def .... Possibly some other package (subcaption?) generates code that is difficult to embed in the command?
    – Joce
    Oct 28, 2021 at 22:52
  • @Joce: Yes, it's trying to expand \xl when writing to the file, and \xl is not defined at that point (it's being defined...). You could try adding \let\xl\relax \let\xhw\relax (and others) as part of the set of commands temporarily set to \relax within suppfigure to avoid their expansion. Try it and let me know; you could also post a most complete, minimal example [here](pastebin.com) and post a link to the code.
    – Werner
    Oct 29, 2021 at 0:40

You must log in to answer this question.

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