[Tex/LaTex] How to write the \columnwidth to an external file

write file

Some figures in my LaTeX document I am generating with Python. Normally I manually check how wide my picture can be with \showthe\columnwidth and adjust the image width accordingly. (This whole is important for not resizing text labels)

Is there a way that I can write this to a file, so I can do this magically? The \columnwidth is always the same through my document, so it only has to be written once. It would be especially nice if the file is not rewritten when the value hasn't changed (good for my Makefile).

Best Answer

This seems to do what you want.

\documentclass[twocolumn]{article}
\newdimen\tempcolwidth \tempcolwidth0pt
\newwrite\widthfile
\AtBeginDocument{\checkcolwidth}
\newcommand\checkcolwidth{%
        \InputIfFileExists{\jobname.width}{}{}%
        \ifdim\columnwidth=\tempcolwidth\else
                \openout\widthfile=\jobname.width
                \write\widthfile{\tempcolwidth=\the\columnwidth}%
                \closeout\widthfile
        \fi
}
\begin{document}
asdf
\end{document}

I should point out that if your file is called foo.tex, then this will produce a file called foo.width which contains the line \tempcolwidth =...pt where the ... is the actual width of the column. If after reading in the file, \columnwidth and \tempcolwidth agree, then the file is not written. Otherwise, the file is opened and the value of the column width written out.

Related Question