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.

5
  • 1
    Gin keys consider only graphicx options, for example width=... or height=... etc.
    – Zarko
    Commented Jul 9, 2018 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
    Commented Jul 9, 2018 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
    Commented Jul 9, 2018 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
    Commented Jul 10, 2018 at 9: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. Commented Dec 27, 2018 at 10:32

1 Answer 1

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}
1
  • nice patch for Gin! +1
    – Zarko
    Commented Jul 10, 2018 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .