# Set default value for “max width” of “includegraphics”

I'd like to set a default value for max width (and max height) for \includegraphics elements.

I have \usepackage[export]{adjustbox} in the preamble so images have the max width key. Then I tried to set the default value like this:

\setkeys{Gin}{max width=\textwidth,max height=0.5\textheight,keepaspectratio}


But it's like it's not there. Applying it directly to the \includegraphics element like \includegraphics[max width=\textwidth,max height=0.5\textheight,keepaspectratio]{...} works.

• Gin keys consider only graphicx options, for example width=... or height=... etc. – Zarko Jul 9 '18 at 15:52
• @Zarko but if you use export, the \adjbox@fam is set to Gin, so the keys provided by adjustbox are in the Gin key family (else you'd get an error because those keys wouldn't be defined). – Skillmon Jul 9 '18 at 16:09
• my claims are based on my experiences. so far i didn't manage that Gin consider adjustbox option. i'm glad to hear, that i'm wrong. – Zarko Jul 9 '18 at 16:19
• @Zarko I just learnt that from viewing the sources. Still for some reasons the setting of adjustbox options outside of \includegraphics doesn't seem to work. – Skillmon Jul 10 '18 at 9:54
• Why not just defining your own new command for it \newcommand\myimage[2][]{\includegraphics[max width=\textwidth,max height=0.5\textheight,keepaspectratio,#1]{#2}} and then use \myimage{imagefilename} or \myimage[extra options]{imagefilename} instead in your document. – Martin Scharrer Dec 27 '18 at 10:32

Instead of redefining \includegraphics one could use etoolbox and \patchcmd. This might be a cleaner solution.

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage{etoolbox}
\expandafter\patchcmd\csname Gin@ii\endcsname
{\setkeys {Gin}{#1}}
{%
\setkeys {Gin}
{max width=\textwidth,max height=.5\textwidth,keepaspectratio,#1}%
}
{}{}

\begin{document}
\includegraphics{./chapterinsection.pdf}
\end{document}


An ugly solution, redefining \includegraphics to include those two keys by default:

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage{letltxmacro}
\LetLtxMacro\includegraphicsBAK\includegraphics
\usepackage{xparse}
\newcommand\MyStarProcessor[1]
{%
\IfBooleanTF{#1}
{\def\ProcessedArgument{*}}
{\def\ProcessedArgument{}}%
}
\RenewDocumentCommand \includegraphics { >{\MyStarProcessor}s O{} o m }
{%
\IfNoValueTF{#3}
{%
\includegraphicsBAK#1
[%
max width=\textwidth,
max height=.5\textheight,
keepaspectratio,
#2
]
{#4}%
}
{%
\includegraphicsBAK#1[#2][#3]{#4}%
}
}

\begin{document}
\includegraphics{./chapterinsection.pdf}
\end{document}

• nice patch for Gin! +1 – Zarko Jul 10 '18 at 10:02