1

I am trying to do cirkuittikz diagram inside a subfile but if i compile main it does not work.

main:

\documentclass[12pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{standalone}
\usepackage{circuitikz}
\usepackage{subfiles}

\begin{document}
    \subfile{include/chapter1}
\end{document}

chapter1.tex:

\documentclass[../main.tex]{subfiles}
\begin{document}
\chapter{Test}
\begin{figure}
    \centering
    \includestandalone{figure/fig}
    \caption{A figure.}
    \label{fig:fig}
\end{figure}
\end{document}

fig.tex:

\documentclass[12pt]{standalone}
\usepackage{circuitikz}
\begin{document}
    \begin{circuitikz}
        \draw[] (0,0)
        to[R]++(0,3)
        ;
    \end{circuitikz}
\end{document}

LaTeX Error: Can be used only in preamble.

You have used a command in the main body of your document which should be used in the preamble. Make sure that \documentclass[…]{…} and all \usepackage{…} commands are written before \begin{document}. Learn more

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.2 \usepackage {circuitikz} Your command was ignored. Type I to replace it with another command, or to continue without it.

) File: figure/fig.tex Graphic file (type tex) ) (/compile/include/chapter2.tex [1

  • I believe that you would simply write \input{figure/fig}. – Bob Sep 27 '19 at 17:14
3

As of version v1.6 of subfiles, this incompatibility of subfiles and the standalone package is fixed, your example now compiles fine. The version is currently only available on Github, but will make it sooner or later also to CTAN (I have to give the people there a break, published too many updates within the last week ;-).

| improve this answer | |
  • Works perfectly. However, standalone needs to be loaded before subfiles. – Max Börjesson Dec 17 '19 at 0:26
  • 1
    Yes, this is right, the docs also say so. The standalone package modifies the \document command, and the subfiles package needs to conserve the modified version to restore it later correctly. As a general rule, subfiles should be loaded last. – gernot Dec 23 '19 at 20:54
1

After looking over this package for the last 45 minutes, I recommend not mixing subfiles and standalone. I have not been able to make them work.

For some reason while loading a \input{standalone} while inside a \subfile{mysubfile} it will print all names of the used packages. This seems like an incompatibility, as by its design, the standalone package is supposed to exclude these. Here is an example of the error.

main.tex

\documentclass[12pt]{report}
\usepackage[mode=tex]{standalone}
\usepackage{circuitikz}
\usepackage[utf8]{inputenc}
\usepackage{subfiles}

\begin{document}

\subfile{chapter1}

And now the figure by itself \input{fig}

\end{document}

chapter1.tex

\documentclass[main.tex]{subfiles}
\begin{document}
\chapter{Test}

\input{fig}

\end{document}

fig.tex

\documentclass[12pt]{standalone}
\usepackage{circuitikz}
\begin{document}
    \begin{circuitikz}
        \draw[] (0,0) to [R] ++(0,3);
    \end{circuitikz}
\end{document}

take note of the erroneous circuitikz

Take note of the erroneous circuitikz word right before the first figure.

As a solution, dont use the standalone package; set your fig.tex file to this, and use the above code for main.tex and chapter1.tex

new fig.tex

\begin{circuitikz}
    \draw[] (0,0) to [R] ++(0,3);
\end{circuitikz}
| improve this answer | |
  • As a side note, I was never actually ever to re-create your error. Which leads me to believe you are either using windows (and it might be a / \ related error), or had your file paths set up wrong. Based upon your inital code you had better had your file structure look like this. root directory |- main.tex | directory named include |- chapter1.tex | directory named figure |- fig.tex where include and figure directories are in the root. – Bob Sep 27 '19 at 18:25
  • Your example (like the one from the OP) now works without errors with subfiles 1.6 or later. Thanks for your investigations. – gernot Oct 31 '19 at 20:16

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.