1

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.

  • 1
    Gin keys consider only graphicx options, for example width=... or height=... etc. – Zarko Jul 09 '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 09 '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 09 '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 09:54
  • 1
    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

1 Answers1

5

New Answer

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

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage[export]{adjustbox}
\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}

Original Answer

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

\documentclass[]{article}

\usepackage[]{graphicx}
\usepackage[export]{adjustbox}
\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}
Skillmon
  • 60,462