Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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.

share|improve this answer
    
Works great! Thanks! –  Peter Smit Dec 16 '10 at 10:44
1  
@Peter: One thing to keep in mind is that TeX uses printer's points which are 72.27pt to 1 inch. Postscript and many other desktop software use 72pt to 1 inch. (TeX supports this latter unit, but calls it a big point. That is 72.27pt = 72bp = 1in.) –  TH. Dec 16 '10 at 11:06
    
Thanks for the hint! I knew it actually (and was already using that measure) but it is probably very useful for anyone browsing by this question. –  Peter Smit Dec 16 '10 at 11:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.