# Linewidth resets when using input files

I'm trying to set some figure files in standalone .tex files, and then input them in the main file. Those files looks something like this

\ifx \plotwidth \undefined
\newlength{\plotwidth} \setlength{\plotwidth}{0.9\linewidth}
\else \fi% width of the total figure
\rule{\linewidth}{\plotwidth}% figure goes here


The main idea I have is to set standard widths using \linewidth, and being able to change the size in the main file by defining the \plotwidth before inputting the file. However, when I call the figures the second time in the same file, for some reason the \plotwidth is defined but its lenght is zero. As this example show

\documentclass{article}

\begin{document}

\begin{figure}
\input{fig.tex}
\caption{Test}
\end{figure}

\begin{figure}
\input{fig.tex}
\caption{Test}
\end{figure}

\end{document}


Can someone explain me why this is not working, and how to solve it?

The problem is that \newlength is a "global" command; so after the first figure, \plotwidth is defined. However, \setlength is a "local" command, so its setting is forgotten after the figure environment and reset to the initial value which is 0pt.

In general \newlength should be a preamble command. You can set it globally to a sentinel value, say -1000pt,

\newlength{\plotwidth}
\setlength{\plotwidth}{-1000pt}


and then check against this value with

\ifdim\plotwidth=-1000pt


in fig.tex. If you say

\begin{figure}
\setlength{\plotwidth}{100pt}
\input{fig}


then the code in fig.tex will know that \plotwidth has been set.

Let's work out an example. Your fig.tex file contains

\ifdim\plotwidth=-1000pt
\setlength{\figwidth}{0.9\linewidth}
\else
\setlength{\figwidth}{\plotwidth}
\fi
\includegraphics[width=\figwidth]{somefigure}


(the parameter will be \figwidth instead of \linewidth, for greater flexibility).

\documentclass{article}

\newlength{\plotwidth}
\setlength{\plotwidth}{-1000pt}
\newlength{\figwidth}

\begin{document}

\begin{figure}
\input{fig.tex}
\caption{Test (this figure is 90\% of the line width)}
\end{figure}

\begin{figure}
\setlength{\plotwidth}{100pt}
\input{fig.tex}
\caption{Test (this figure is 100pt wide)}
\end{figure}

\end{document}

• Instead of defining with a fixed value, is there any other way of creating if it doesn't exist and then forgetting it in the file? I remember that there is a \providelenght in the pstricks that does something like that. But is there any general package that does something similar? – adn Oct 11 '11 at 14:06
• @adn You don't need to create \plotwidth each time; it's only necessary to set its value. I'll expand my answer. – egreg Oct 11 '11 at 14:09
• Thanks @egreg. However, my idea is to be able to add this figures without worrying about their widths. And if for some reason I need to change them later, just to change the width for that image. That's why I check if the length already exists. But, I need some automatic way of checking if it exist, and I prefer not to use extra code in the main document. Is it possible? or I must initialize the length and then check as you mention in your post. – adn Oct 12 '11 at 4:48
• Hi, I solve it by checking against 0pt, that will mean that the length is new from the other files. Thanks! :) – adn Oct 12 '11 at 5:20

The problem is that \newlength is global but \setlength is local. The first time your main document inputs fig.tex \plotwidth is undefined so the new length is created and set. This happens inside a TeX group so the setting is local to the input file. Then that group ends and \plotwidth reverts to its initial setting of 0.0pt.

The second time the name document inputs fig.tex \plotwidth is defined, so the \setlength branch is not expanded. The result is that \plotwidth remains at 0.0pt.

The solution is to give \plotwidth a value in the main tex file.

Also, if you want your fig.tex file to be standalone tex file, the easiest way to do that is to use the standalone document class and package. Here is your MWE with that:

fig.tex:

\documentclass{standalone}
\begin{document}
\ifx \plotwidth \undefined
\newlength{\plotwidth} \setlength{\plotwidth}{0.9\linewidth}
\else \fi% width of the total figure

\rule{\plotwidth}{\plotwidth}% figure goes here
\end{document}


main.tex:

\documentclass{article}
\usepackage{standalone}

\newlength{\plotwidth} \setlength{\plotwidth}{0.5\linewidth}
\begin{document}

\begin{figure}
\include{fig}
\caption{Test}
\end{figure}

\begin{figure}
\include{fig}
\caption{Test}
\end{figure}

\end{document}


\showthe\plotwidth in your file will halt expansion and show you the current value of \plotwidth. Very good for debugging.

I notice as I finish this that egreg has answered as well. I basically agree with him.

• I understand the problem now. However, I want to have the ability to forget about the size of the figure and leave it with a default value. Is there any option to set the length globally? or at least delete the length at the end of the fig.tex file? So next time it enters the file won't be any length defined. – adn Oct 11 '11 at 13:57
• @adn If you do \setlength{\plotwidth}{...} in the figure environment this value will be forgotten in when the figure environment ends. – egreg Oct 11 '11 at 14:08