5

I'm a copy-editor and in my job I format papers for scientific journals. One of my task is (in some cases) to crop images to remove extra space between the images and the bounding box.

Usually I use to \fbox to visually check the amount of space between the images and the bounding box, but to do that I need to wrap each \includegraphics command with \fbox{...}.

My question is, is there a way to box all the images by default? Maybe using \usepackage[export]{adjustbox} in the preamble and pass the frame option to \includegraphics command by default.

Gabriele
  • 1,815

3 Answers3

9

You could redefine \includegraphics to use the frame-option like this:

\documentclass[]{article}

\usepackage[export]{adjustbox}

\let\includegraphicsbak\includegraphics
\renewcommand*{\includegraphics}[2][]{\includegraphicsbak[frame,#1]{#2}}

\begin{document}
\includegraphics[height=4cm]{example-image}
\end{document}

Another possibility is to redefine it to always use \fbox around it with

\renewcommand*{\includegraphics}[2][]{\fbox{\includegraphicsbak[#1]{#2}}}

In the latter case you'll always get the small gaps because of the \fboxsep-length (which you could reduce to 0pt but this way you might break things in the document you edit).

EDIT: You might need \LetLtxMacro for the command, take a look here for explanations of \LetLtxMacro.

Skillmon
  • 60,462
3

A slightly different version to skillmon's solution, by defining a framed document/package option that and redefinition of includegraphics, that uses this option.

extgraphicx loads graphicx and passes the other options to that package.

This way \documentclass[frame]{article} is sufficient.

\begin{filecontents}{extgraphicx.sty}
\ProvidesPackage{extgraphicx}

\newif\ifframeused
\RequirePackage{xkeyval}
% Define some option
\define@boolkey{extgraphicx.sty}[Gin@]{frame}[true]{%
  \typeout{Used}
}

%Pass the other unknown thingies to graphicx
\DeclareOptionX*{\PassOptionsToPackage{\CurrentOption}{graphicx}}

\ProcessOptionsX*

\RequirePackage{graphicx}




\RequirePackage{xparse}


\let\graphicx@@includegraphics\includegraphics

\RenewDocumentCommand{\includegraphics}{O{}m}{%
  \begingroup
  \ifGin@frame
  \fbox{%
    \graphicx@@includegraphics[#1]{#2}%
  }%
  \else
  \graphicx@@includegraphics[#1]{#2}%    
  \fi
  \endgroup
}

\end{filecontents}



\documentclass[frame=false]{article}



\usepackage{extgraphicx}



\begin{document}
\includegraphics{ente}

\end{document}
3

If you use \adjincludegraphics instead of the normal \includegraphics then you can use \adjustboxset to set default keys for all images (but also all other \adjustboxs).

\documentclass{article}

\usepackage{adjustbox}

\adjustboxset*{frame=5pt} % * to add frame behind local keys, not before

\begin{document}
\adjincludegraphics[height=4cm]{example-image}
\end{document}
Martin Scharrer
  • 262,582
  • I expected that using \usepackage[export]{adjustbox} would allow \adjustboxset to work directly with \includegraphics. Is this not true? – Ciprian Tomoiagă Feb 12 '19 at 15:04
  • 1
    @CiprianTomoiagă: No, the export option just makes the key namesspace the same so \includegraphics gets the same keys. As the key list given by \adjustboxset must be processed internally and \includegraphics (which is not changed by adjustbox) does not do this these keys only work with \adjincludegaphics or \adjustimage. I recommend the later for new documents. – Martin Scharrer Feb 13 '19 at 06:37