4

I am trying to use the standalone package to break up my large document into smaller ones.

The structure of my document currently is:

main.tex

\documentclass{article}
\usepackage{packages} %custom .sty file

\begin{document} % start the body of the text
..<content>..
\input{overview}

\newpage
\printbibliography
\end{document}

overview.tex

\documentclass[crop=false]{standalone}
\usepackage{packages}

\begin{document}
\onlyifstandalone{\section{Overview}}
  ..<content>..\cite{citation1}
  \input{OverviewSubsection}
\label{label:Overview}

\onlyifstandalone{\printbibliography} %print references if compiled from here
\end{document}

OverviewSubsection.tex

\documentclass[crop=false]{standalone}
\usepackage{packages}

\begin{document}
\subsection{ubsection To Overview}=
..<content>.. \cite{citation2}
\label{sub:OverviewSubsection}

\onlyifstandalone{\printbibliography}%print references if compiled from here
\end{document}

packages.sty contains the packages i need for main and all subfiles and looks like this:

\usepackage{standalone}

\usepackage[letterpaper,margin=1in]{geometry} % adjust page dimensions and orientation

..<other package>..

%biblatex, must use backend=bibtex in order to compile under sublime correctly - raises a warning
\usepackage[style=ieee]{biblatex}
%bibliography files
\addbibresource{reference1.bib}
\addbibresource{references2.bib}
\addbibresource{references3.bib}

The Problem
When I compile main.tex there is only a single references section which is good as it is properly formatted and contains all references, i assume this is because this document has the class article. But when i compile Overview.tex there are two references sections one after the other which are exactly the same and contain citation1 and citation2.
When i compile OverviewSubsection.tex there is only 1 references section which is obviously because it doesn't call anything inside it.

How do i make it so i can compile Overview.tex and have it input OverviewSubsection.tex correctly such that OverviewSubsection.tex does not evaluate the \onlyIfStandalone{} block?

I have tried using

\ifstandalone
\printbibliography
\fi

instead of \onlyIfStandalone{} and it has the exact same behavior.

In addition when i attempt to use \includeStandalone[]{} instead of \input{} it throws many errors including Something's wrong--perhaps a missing \item, Missing } inserted. [\end{document}],Missing } inserted. [\end{document}],Extra }, or forgotten \endgroup and many more errors even though it compiles fine (albeit with two references sections) with \input{}

I also thought this error might have something to do with the fact that all files are using packages.sty which itself uses standalone but this shouldn't be the issue.

11
  • This might help you: tex.stackexchange.com/a/352459
    – Ross
    Feb 28 '17 at 23:44
  • @Ross this doesn't seem to work as i get Incomplete \iffalse; all text was ignored after line 7 evaluated when i try to import OverviewSubsection and compilation throws an error.
    – Ssxxkk
    Feb 28 '17 at 23:56
  • @Ross adding a /fi to the end of \newif \ifstandlone \standalonetrue inside the OverviewSubsection allows Overview to compile with only a single references section but it doesn't seem to evaluate to a standalone itself as the section header inside the \onlyifstandalone doesn't show up, in addition i can trace the references created on the document to the Oversubsection instead of Overview which is not right, the Overview should be creating the references, not the subsection since the subsection is a child of Overview. Very confusing.
    – Ssxxkk
    Mar 1 '17 at 0:03
  • A belated welcome to TeX.SE. It is difficult to tell with out a MWE that we can compile and see what is happening. Can you construct one, like the one in the link I gave in the comment? Standalone can be nested (see tex.stackexchange.com/questions/30784/…) so, in principle, it is possible. You might need to be careful identifying the root file to be compiled e.g. % !TeX root = \jobname.tex, although this may not work on all installations.
    – Ross
    Mar 1 '17 at 1:36
  • Why would you think \includestandalone suitable here? Have you read the package's documentation?
    – cfr
    Mar 1 '17 at 2:39
1

The question and discussion reflects a fundamental misunderstanding of how the conditionals provided by standalone work.

As the manual explains it,

  • the class sets \ifstandalone to \iftrue;

  • the package sets \ifstandalone to \iffalse.

Whether it is true or false is not something which varies for the inclusion of one file versus another. It is either true or false throughout.

Here's a demonstration.

\begin{filecontents}{level1.tex}
\documentclass{standalone}
\usepackage{standalone}
\begin{document}
  \ifstandalone True at level 1.
  \else False at level 1.
  \fi
  \input{level2}
\end{document}
\end{filecontents}
\begin{filecontents}{level2.tex}
\documentclass{standalone}
\begin{document}
  \ifstandalone True at level 2.
  \else False at level 2.
  \fi
\end{document}
\end{filecontents}
\documentclass{article}
\usepackage{standalone}
\begin{document}
  \ifstandalone True at level 0.
  \else False at level 0.
  \fi
  \input{level1}
\end{document}

Compiling this file first produces

false always

Here, the condition is never satisfied. It doesn't matter whether we are in the main document, the first included file or the second included file included by the first. \ifstandalone is always \iffalse.

If we now compile level1.tex, we get

true always

Here the condition is always satisfied. It doesn't matter whether we are at the top level or in the included file. \ifstandalone is always \iftrue.

Finally, compiling level2.tex produces

true

Here the condition is satisfied. \ifstandalone is \iftrue.

You are expecting \ifstandalone to change from \iftrue to \iffalse according to the file currently being read. But that is not how this is designed to work. Being in standalone mode or not being in it is a property of the entire document environment from \begin{document} to \end{document}. It is not supposed to change in between.

This is simply a feature of the way standalone is designed. \ifstandalone does what it says on the standalone tin. You appear to have chosen the wrong tin. Either resign yourself to baked beans rather than treacle, or take it back to the shop and ask to exchange it for treacle. But you will doubtless need to pay the difference in price if you choose the latter option, treacle generally being more costly than baked beans.

EDIT

I really don't see that this is such a big deal. All you need is a conditional to do what you want. standalone already does the hard bit of ignoring preambles and so on. The fact that it doesn't provide an out-of-the-box conditional defined to meet your particular needs is hardly a glaring defect. Just define one.

\begin{filecontents}{iflevel.tex}
\newif\iftoplevel
\topleveltrue
\newcommand*\inputlevel[1]{%
  \begingroup
    \toplevelfalse
    \input{#1}%
  \endgroup
}
\end{filecontents}
\begin{filecontents}{level1.tex}
\documentclass{article}
\usepackage{standalone}
\input{iflevel}
\begin{document}
  \iftoplevel True at level 1.
  \else False at level 1.
  \fi
  \inputlevel{level2}
\end{document}
\end{filecontents}
\begin{filecontents}{level2.tex}
\documentclass{article}
\input{iflevel}
\begin{document}
  \iftoplevel True at level 2.
  \else False at level 2.
  \fi
\end{document}
\end{filecontents}
\documentclass{article}
\usepackage{standalone}
\input{iflevel}
\begin{document}
  \iftoplevel True at level 0.
  \else False at level 0.
  \fi
  \inputlevel{level1}
\end{document}

Compiled as is:

top level

level1.tex compiles to:

first level

level2.tex compiles to:

second level

There is certainly a more elegant way to do this. If you get the current file name, you can test against \jobname and avoid the need for a distinct input macro.

Note that packages are created by people in response to itches. If you have an itch nobody has scratched, roll your own and put it on CTAN. It is no use being disappointed that nobody else has done what you are unwilling to do yourself.

5
  • i dont quite understand your treacle/beans analogy but im glad you are are at least getting the same result as me. I'm quite disappointed that no one has ever created a latex package (or even that latex doesn't have this built-in) where you can create standalone documents nested at multiple levels. the hierarchy seems very natural.
    – Ssxxkk
    Mar 2 '17 at 16:50
  • @Ssxxkk So write one. No point complaining nobody else has done what you aren't prepared to do yourself. I don't see it is a big deal, personally. Just define a conditional and input command for a quick, if not very elegant, solution. Or develop a more elegant approach if you use this a lot, according to your workflow. Then fill the gaping hole in current provision by putting it on CTAN.
    – cfr
    Mar 2 '17 at 17:23
  • i just dont really know enough about latex under the hood to write one. I am still a latex novice. If you think this would be quick to define to get the nested document that also compile by themselves correctly then i'd appreciate your help in defining one.
    – Ssxxkk
    Mar 2 '17 at 17:37
  • i see you edited your post and included a solution. Can you explain to me how \iftoplevel works? I can't parse it.
    – Ssxxkk
    Mar 2 '17 at 17:45
  • @Ssxxkk It is just a standard \newif like any other. Set true in the preamble; set false by the \inputlevel macro for the duration of that group. So it is true for the main document except when reading a file included with \inputlevel.
    – cfr
    Mar 2 '17 at 18:16
0

EDITED 2-Mar-17 to load overviewsubsection.tex from overview.tex

This answer is developed from this one (https://tex.stackexchange.com/a/30000) which considered using standalone with a conditional for a title page in the subfile.

The following MWE produces the following files:

  1. main.tex - this is the master file that uses \input to load the files containing the section and subsection contents
  2. overview.tex - this contains the section content with two citations
  3. overviewsubsection.tex - this contains the subsection content with one citation, which is loaded by overview.tex
  4. refs1.bib - the bibresource file used by overview.tex
  5. refs2.bib - the bibresource file used by overviewsubsection.tex

Run pdflatex on the MWE to produce each of these files. Then you can run pdflatex > biber > pdflatex on main.tex (as well as overview.tex and overviewsubsection.tex, if desired, in which case Overview.tex and overviewsubsection.tex each have their own bibliography. The MWE and output from compiling main.tex are as follows.

\begin{filecontents*}{overview.tex}
\documentclass[a4paper,10pt,preview=false]{article}
\usepackage{standalone}
\usepackage[backend=biber,style=ieee]{biblatex}
\usepackage{lipsum}
\addbibresource{refs1.bib}

\begin{document}
    \section{Overview}
    Some text here \cite{citation1} and another citation \cite{citation2}
    \label{label:Overview}
    \input{overviewsubsection}
    \ifstandalone
    \printbibliography %print references if compiled from here
    \fi
\end{document}
\end{filecontents*}

\begin{filecontents*}{overviewsubsection.tex}
\documentclass[a4paper,10pt,preview=false]{article}
\usepackage{standalone}
\usepackage[backend=biber,style=ieee]{biblatex}
\usepackage{lipsum}
\addbibresource{refs2.bib}

\begin{document}
    \subsection{subsection To Overview}
    Some text here \cite{citation3}
    \label{sub:OverviewSubsection}
    \ifstandalone
    \printbibliography %print references if compiled from here
    \fi
\end{document}
\end{filecontents*}

\begin{filecontents*}{refs1.bib}
    @article{citation1,
        author={Jane Doe},
        year={2010},
        title={Title 1}
    }
    @article{citation2,
    author={Mike Smith},
    year={2010},
    title={Title 2}
    }
\end{filecontents*}

\begin{filecontents*}{refs2.bib}
    @article{citation3,
        author={Mark Bart},
        year={2004},
        title={Title 3}
    }
\end{filecontents*}

\begin{filecontents*}{main.tex}
\documentclass[preview=false]{article} %main.tex
\usepackage{standalone}
\usepackage[backend=biber,style=ieee]{biblatex}
\usepackage{lipsum}
\addbibresource{refs1.bib}
\addbibresource{refs2.bib}

\begin{document} % start the body of the text
    \input{overview}
    \printbibliography
\end{document}
\end{filecontents*}

\documentclass{article}
\begin{document}

\end{document}

enter image description here

7
  • \newif\ifstandlone \standalonetrue only works because the package already defines the conditional you are trying to use. The new one you create is never used at all.
    – cfr
    Mar 1 '17 at 15:09
  • @Ross the solution you suggest doesn't allow me to compile Overview fully as it would not include OverviewSubsection inside it which is a requirement for compiling a correct Overview
    – Ssxxkk
    Mar 1 '17 at 16:47
  • @Ross i ran your edited and updated tex for overview.tex and it still add two references sections. It is as if it ignores the fact that overviewsubsection.tex is not standalone when called in overview.tex
    – Ssxxkk
    Mar 2 '17 at 1:10
  • here is an image of the output when i compile overview.tex: i.imgur.com/FBNWqiT.png
    – Ssxxkk
    Mar 2 '17 at 1:22
  • You might as well delete the \ifstandalone ... \fi as they aren't doing anything at all. The condition is always false.
    – cfr
    Mar 2 '17 at 5:02

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.