2

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:

enter image description here

Expected behaviour:

enter image description here

Currently bad output:

enter image description here

Basj
  • 764

1 Answers1

3

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}

enter image description here


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.