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

  • Maybe try to place your image in the \marginpar{}? – user36296 Oct 19 '18 at 9:21
  • 1
    You should really not use $$ – user36296 Oct 19 '18 at 9:21
  • @samcarter better \marginnote (better placement). – Skillmon Oct 19 '18 at 9:22
  • @Skillmon OK, I'll reformulate to "something starting with \margin" :) – user36296 Oct 19 '18 at 9:23
  • @Skillmon Do you want to answer? – user36296 Oct 19 '18 at 9:24
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.

  • 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
  • 1
    @Basj Yes, it is placed behind the text. It will cause an overfull box warning though. – user36296 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
  • 1
    @Basj You can adjust these spaces with the geometry package where you can set marginparwidth and marginparsep – user36296 Oct 19 '18 at 9:46
  • 1
    @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. – user36296 Oct 19 '18 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.