# Automatic width setting in LyX

Is there any preference setting so that LyX automatically adjusts images width to \linewidth when they are added, instead of doing so by hand whenever a new image is inserted?

• Yes, exactly what I wanted, thank you very much @Werner! – nonKreon Jan 5 '14 at 17:19
• Also consider using graphics groups. See Help > User Guide, Section 4.4.2 Grouping of Image Settings – scottkosty Oct 27 '14 at 1:18

You can set the keys associated with \includegraphics globally in your LaTeX Preamble by adding to it

\setkeys{Gin}{width=\linewidth}


This will resize all images (either enlarging or shrinking) to make them fit to \linewidth. If you only wish to shrink image that are too large, you can use (from Resize images to half of text width only if too big for page margin):

\makeatletter
\setkeys{Gin}{width=\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi}
\makeatother


Here \Gin@nat@width is the natural width of the image to be included.

• This works perfectly if there are figure floats, otherwise causes latex to crash. How can I put this in an if statement such that if there is at least a figure float \setkeys{Gin}{width=\linewidth} else do nothing \fi – nonKreon Jun 13 '17 at 20:30
• @BerkerPeköz: That should not be the case. No figure floats typically mean no \includegraphics and therefore no use of the Gin family of keys. One can test for the number of figures, say, in a document, but that should not be necessary here. Perhaps you can show your code to identify where you may be going wrong...? – Werner Jun 13 '17 at 20:40
• thanks for your quick response after all these years :) A simple MWE would be \documentclass{IEEEtran} \setkeys{Gin}{width=\linewidth} \begin{document} This is a MWE \end{document} If a figure float is present, the command runs beautifully but if not, it causes pdflatex to crash. I've been commenting it out when there is no figure and enable it as I add one, but I'm trying to automate this process with an if statement. – nonKreon Jun 14 '17 at 15:37
• @BerkerPeköz: Ahhh. LyX inserts the use of the graphicx package if there are images included in the document, but otherwise removes it. Just add \usepackage{graphicx} to your Document > Settings... > LaTeX Preamble before the \makeatletter...\makeatother addition I specify in the answer. – Werner Jun 14 '17 at 16:28
• now it's robust to not having figures, and the solution is simple and beautiful it doesn't even need if statements. thank you so much :) – nonKreon Jun 15 '17 at 15:27