1

I have one main document and two standalone files of tikz pics as given in this answer. I need to have \newlength in the main document and all the standalone files in order to be able to run any document on its own without the need to change the main document structure every single time.

However, relaxing lengths as shown in this answer doesn't work for me. So, how can both cases be integrated to work flawlessly?

Main Document

\documentclass{article}
\usepackage{tikz}
\usepackage[subpreambles=true]{standalone}% I need subpreambles to be true
\begin{document}

% I need to keep the following definitions
\newlength{\mylength}
\setlength{\mylength}{1cm}
\let\mylength\relax

\begin{figure}
  \includestandalone[width=\textwidth]{myfrsttikz}
  \caption{My First TikZ picture}
\end{figure}

\begin{figure}
    \includestandalone[width=\textwidth]{mysecondtikz}
    \caption{My Second TikZ picture}
\end{figure}
\end{document}

First Standalone TikZ File

% myfrsttikz.tex %

\documentclass{standalone}
\newlength{\mylength}
\setlength{\mylength}{2cm}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) -- (\mylength,\mylength);
\end{tikzpicture}
\end{document}

Second Standalone TikZ File

% mysecondtikz.tex %
\documentclass{standalone}
\newlength{\mylength}
\setlength{\mylength}{5cm}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) -- (\mylength,\mylength);
\end{tikzpicture}
\end{document}
  • @UlrikeFischer Thanks for referring me to this answer. I still don't quite know how to edit the standalone files accordingly after defining \providelength in the main document. – Diaa Aug 8 '18 at 13:50
  • 1
    \let\mylegnth\relax effectively deletes the definition of \mylength. I'm not familiar with either \includestandalone or subpreamble, but it seems you might want to wait before deleting \mylength. – John Kormylo Aug 8 '18 at 14:40
2

The easiest way to transmit definitions from the main source to other files is to use filecontents and \input. \includestandalone seems like an unnecessary complication.

Main source:

\begin{filecontents}{data}
\newlength{\mylength}
\setlength{\mylength}{1cm}
\end{filecontents}
%
\documentclass{article}
\usepackage{tikz}
%\usepackage[subpreambles=true]{standalone}% I need subpreambles to be true
\begin{document}

\begin{figure}
  \includegraphics[width=\textwidth]{test5}% renamed
  \caption{My First TikZ picture}
\end{figure}

\end{document}

First standalone:

\documentclass{standalone}
\usepackage{tikz}

\input{data}

\begin{document}
\begin{tikzpicture}
  \draw (0,0) -- (\mylength,\mylength);
\end{tikzpicture}
\end{document}

Of course, in practice one will not add the \includegraphics until after the standalone has been run.

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.