# Add a grid on figures for trimming purpose (by redefining \includegraphics)

I need to (temporarly) redefine \includegraphics command for trimming purposes. This is my MWE:

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage[abs]{overpic}
\pagestyle{empty}
\begin{document}
\begin{figure}[h]
\centering
\resizebox*{1\textwidth}{!}{\begin{overpic}[grid,unit=1pt]{lemon.jpg}
\end{overpic}}\relax

\includegraphics[trim = 0 100 0 100, clip, width=1\textwidth]{lemon.jpg}
\caption{\label{myfig}My caption.}
\end{figure}

\end{document}


In this example I have a picture inserted with the overpic environment that create a grid on my figure. This allow my to find, quickly, the values to pass to the trim parameter of the \includegraphics command.

To make this operation much less "overkill" I tried to redefine the \includegraphics command this way:

\renewcommand*{\includegraphics}[2][]{\resizebox*{1\textwidth}{!}{%
\begin{overpic}[grid,unit=1pt]{#2}\end{overpic}}\relax}


but it doesn't work. I get this error:

! TeX capacity exceeded, sorry [grouping levels=255]....


I know you may find it yet "overkill" but I have my goog reasons to do that.(This would allow me to pass this redefinition by command line, keeping my .tex untouched)

My answer(s) is "how can I redefine the \includegraphics command to insert this sort of grid in my figures? What's wrong in my code?"

I took the idea from this post: https://latex.org/forum/viewtopic.php?t=7783

Edit. I can't understand why if i define this command(s)

\newcommand{\vacua}[1]{}
\newcommand*{\myincludegraphics}[2][]{\resizebox*{1\textwidth}{!}{%
\begin{overpic}[grid,unit=1pt]{#2}\end{overpic}}\relax\vacua{#1}}


the string:

\myincludegraphics[trim = 0 100 0 100, clip, width=1\textwidth]{lemon.jpg}


generates my picture with the grid correctly. But if i do:

\newcommand{\myignore}[1]{}
\let\includegraphics\undefined %% I undefine "\includegraphics"
\newcommand*{includegraphics}[2][]{\resizebox*{1\textwidth}{!}{%
\begin{overpic}[grid,unit=1pt]{#2}\end{overpic}}\relax\myignore{#1}}


with the string:

\includegraphics[trim = 0 100 0 100, clip, width=1\textwidth]{lemon.jpg}


I keek getting the same error.

• Circular definition, overpic uses includegraphics internally. You're better of making a personal macro and then replacing it when you are done trimming. – daleif Oct 14 '18 at 16:49
• Again circular definition. Overpic uses includegraphics internally, so you get includegrapgics, calling overpic, calling includegrapgics, calling overpic.... – daleif Oct 14 '18 at 17:13
• Your myincludegraphics is a bit strange. Why are you throwing away the optional argument (#1). Instead of passing it on to overpic? – daleif Oct 14 '18 at 17:15
• There is another possibility with tikz: tex.stackexchange.com/q/9559/138900 – AndréC Oct 14 '18 at 17:35
• @daleif If I pass it to overpic I'll get wrong values... look at latex.org/forum/viewtopic.php?t=7783 – Gabriele Nicolardi Oct 14 '18 at 17:51

To avoid the circular definition (as pointed by daleif) I solved using this trick:

1. I replace "\includegraphics" with "\myincludegraphics" in my .tex (easy to do with emacs)
2. I compile with these options:



Now I get a grid on all the picture inserted with the \includegraphics command and I can use the trim parameter to tune the layout having a visible estimation of the trimming values (expressed in pt).

3. I restore the \includegraphics string in my .tex file.

The \resizebox stuff helps me to obtain a temporarly enlarged layout of the figures...

The \myignore trick is required to obtain a real estimation of the trimming values (see https://latex.org/forum/viewtopic.php?t=7783)

I wrote a short elisp function that does all these operations (points 1-3) in one shot (I love emacs).

• DON'T use src-specials. They disrupt the typesetting for no purpose whatsoever. – egreg Oct 15 '18 at 15:52
• @egreg I removed the src-specials stuff. It's not relevant in this case. – Gabriele Nicolardi Oct 16 '18 at 6:56