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.

2
  • Using the draft you would see how large the image is, however not the real white space
    – user31729
    Commented Jun 15, 2017 at 7:16
  • @ChristianHupfer I need to see the box "and" the image at the same time.
    – Gabriele
    Commented Jun 15, 2017 at 7:23

3 Answers 3

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.

1
  • Both solutions work fine for my purposes. Thanks.
    – Gabriele
    Commented Jun 15, 2017 at 7:51
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}
4
  • Very nice solution, but maybe a bit overkill? :)
    – Skillmon
    Commented Jun 15, 2017 at 8:28
  • 1
    @Skillmon: A toy solution ;-)
    – user31729
    Commented Jun 15, 2017 at 8:29
  • Does \includegraphics need \LetLtxMacro? It didn't appear to me that way when I checked.
    – Skillmon
    Commented Jun 15, 2017 at 8:31
  • @Skillmon: I did not check -- there's a starred variant, however. I think, I can omit it
    – user31729
    Commented Jun 15, 2017 at 8:33
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}
2
  • I expected that using \usepackage[export]{adjustbox} would allow \adjustboxset to work directly with \includegraphics. Is this not true? Commented Feb 12, 2019 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. Commented Feb 13, 2019 at 6:37

You must log in to answer this question.

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