# Include a graphics but without modifying the layout (i.e. add an image behind text)

I'd like to define a command \mark that would add an image mark.png :

• on the same vertical coordinate than the current text
• at 1cm from left border
• without modifying the layout, i.e. if I remove this command, the text shouldn't move, the pages should have the same layout

Example

\documentclass{article}
\usepackage{graphicx}
\def\mark{\includegraphics{test.png}}
\begin{document}
Hello world
$$x^2+1$$
\mark Hello world
\end{document}


Without:

Expected behaviour:

You could place your image in the margin, this ensures that it does not influence the layout of the main text.

To do this you could use \marginpar{....} or as @Skillmon suggested in the comments \marginnote{} from the package of the same name for better placement.

Unrelated to the problem, but you should not use $$...$$, see Why is $...$ preferable to $$...$$? for further information.

\documentclass{article}
\usepackage{graphicx}

\usepackage{marginnote}
\reversemarginpar

\begin{document}
Hello world
$x^2+1$
\marginnote{\includegraphics[width=1cm]{example-image-duck}}
Hello world
\end{document}


Since @samcarter allowed me to, here are some edits I'd use:

1. I'd put the macro into a \begingroup-\endgroup pair and keep the \reversemarginpar into that group, so that we don't affect other macros using it.

2. I'd put the \includegraphics into an \rlap so that it doesn't matter how wide the image is.

3. I'd change the alignment of \marginnote put into the left margin locally.

Put into code:

\newcommand\mymark[2][]
{%
\begingroup
\reversemarginpar
\renewcommand*\raggedleftmarginnote{\raggedright}%
\marginnote{\rlap{\includegraphics[#1]{#2}}}%
\endgroup
}


Now you can use \mymark as you'd do with \includegraphics but putting the image into the margin.

Note: Don't forget to place it inside equations, if you want proper alignment:

 $\mymark x^2 +1$


works, whereas

 \mymark $x^2 +1$


would produce a misalignment.

• If the image is large (say, 10 cm), can it go behind the text, without modifying the text's position? – Basj Oct 19 '18 at 9:36
• @Basj Yes, it is placed behind the text. It will cause an overfull box warning though. – samcarter is at topanswers.xyz Oct 19 '18 at 9:38
• Also, could you just add a mention to explain what the reverse does? (\reversemarginpar) Thanks in advance! – Basj Oct 19 '18 at 9:38
• @Basj You can adjust these spaces with the geometry package where you can set marginparwidth and marginparsep – samcarter is at topanswers.xyz Oct 19 '18 at 9:46
• @Basj With the link to the other question I just wanted to show that if you want to annotate an equation, then the margin note should be placed there and not before the equation. – samcarter is at topanswers.xyz Oct 19 '18 at 15:18