# Automate width determination for included graphics

For including graphics such as Matlab plots, I use \includegraphics from the graphicx package. Every time, I have to decide on some width and pass e.g. [width=180mm] to the inclusion command. This feels very arbitrary. How would I let LaTeX find the optimal width, assuming the graphic scales nicely without pixelation?

-
How do you define “correct”? The same as \textwidth? –  Caramdir Mar 6 '11 at 18:05
@Caramdir: I just want it to look nice. \textwidth would probably be fine. –  Tim N Mar 6 '11 at 18:09

Just like Caramdir suggests, use \hsize or \textwidth to scale your graphics to a consistent width:

\usepackage{graphicx}
...
\includegraphics[width=0.8\hsize]{some-file}


You could also define a dimension for flexibility that you could later easily re-define to a different width:

\newdimen\photowidth
\photowidth=0.8\textwidth % or \photowidth=180mm
...
\includegraphics[width=\photowidth]{some-file}

-
Good answer, but \photowidth doesn't need to be a dimension but can also be a macro which holds e.g. 0.8\textwidth. This has the benefit (or drawback, depending on the point of view) that it adjusts if \textwidth changes afterwards, e.g. inside a minibox. –  Martin Scharrer Mar 6 '11 at 18:47
@Martin: I assume the advantage of a dimension is that you would use it as in [width=0.5\photowidth] but this wouldn't be possible if it were defined as macro, right? But I can see your point, too -- a dimension is defined once. –  Christian Lindig Mar 6 '11 at 18:52
You are right, but eTeX comes to the rescue: \def\photowidth{\dimexpr .8\textwidth\relax} gives you both the advantages! –  Martin Scharrer Mar 6 '11 at 18:58
@Cristian Lindig: \newdimen is TeX command. How does it differ from \setlength? I prefer not to mix LaTeX and TeX commands just to keep my co-authors from asking the same question: what is the difference? –  Igor Kotelnikov Mar 7 '11 at 2:56
@Igor You are right. To stay within LaTeX you can use \newlength{photowidth} to introduce the new dimension and use \setlength{photowidth}{180mm} to set it. –  Christian Lindig Mar 7 '11 at 7:47

More elegant solution is to set desired width globally:

\setkeys{Gin}{width=0.75\textwidth}


Setting width locally as shown by Christian overrides global keys.

-