1

I would like to add some picture to an existing one that I annotate. For that, I do it in tikz with the command

\node[draw=black,anchor=south west] at (0,0.4){\includegraphics{ex.png}};

Note that I added the black rectangle only for clarity, in order to see what are approximately the limits of the figure. This is removed in my final thing. Now, as you can see, the figure looks bad enter image description here

I'm wondering if it's possible to crop the figure around these dashes lines, in order to be able to see the rest of the picture.

Here is the code I use to generate this image :

\documentclass{standalone}
\usepackage{graphicx} %Manage images of different formats
\usepackage{tikz}   % drawing

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{NuclideMap2.png}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
    \draw[->] (0.014,0) -- (1,0);
    \draw (1,0) node[anchor= west]{$N$};
    \draw[->] (0,0.02) -- (0,1);
    \draw (0,1) node[anchor=north west]{$Z$};
    \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
    \node[draw=black,anchor=south west] at (0.01,0.4){\includegraphics[width=0.35\textwidth]{ex.png}};
    \draw[thick,dashed] (0.2,0.35) -- (0.45,0.85);
    \draw[thick,dashed] (0.005,0.45) -- (0.08,0.98);
    \end{scope}
    \end{tikzpicture}
\end{document}
  • 3
    You can probably do this by using a clip : 1. open a scope 2. define your clipping area \clip <closed path> 3. put your node with the pic. 4. close the scope. – marsupilam Aug 18 '17 at 10:30
  • Another option that may work is to make the png background transparent in an image editor. – Marijn Aug 18 '17 at 10:38
  • @marsupilam How would that look exactly? I'm quite new to tikz I must say. – mwoua Aug 18 '17 at 10:42
  • 2
    @mwoua it would probably be a bit easier to give examples if you provide a full code og how you made this image. Then the rest is very easy. Remember everyone here are volunteers, so help us help you. – daleif Aug 18 '17 at 10:55
  • 1
    Related (duplicate?): tex.stackexchange.com/questions/19938/… – Jake Aug 18 '17 at 11:08
1

Here is one example of clipping, to help you understand the ideas in dexteritas answer (and maybe even make do without it ? ;))

The output

enter image description here

vs

enter image description here

The code

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}

\strut\vspace{10cm}

\begin{tikzpicture}[transform canvas={scale=10}]
  \node[blue,anchor=base west,inner sep=1pt]{B};
  %\node[red,anchor=base,scale=2]{A}; % this would hide part of the B
  \begin{scope}
    \clip (0,-1ex) rectangle (-1em,5ex); %try \clip[draw] to see the clipped region
    \node[red,anchor=base,scale=2]{A} ;
  \end{scope}
\end{tikzpicture}
\end{document}

Let's apply to your case.

The ouput

enter image description here

The code

\documentclass[tikz]{standalone}
\usepackage{graphicx} %Manage images of different formats

\begin{document}

\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[->] (0.014,0) -- (1,0);
        \draw (1,0) node[anchor= west]{$N$};
        \draw[->] (0,0.02) -- (0,1);
        \draw (0,1) node[anchor=north west]{$Z$};
        \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
        \begin{scope}
          \clip (0.2,0.35) -- (0.45,0.85) -- (0.08,0.98) -- (0.005,0.45) -- cycle; % closed path to clip
          \node[anchor=south west] at (0.01,0.4){\includegraphics[width=0.35\textwidth]{example-image-b}};
        \end{scope}
    \end{scope}
\end{tikzpicture}

\end{document}
  • Thank you for this pedagogical way of explaining it too. Much appreciated :) – mwoua Aug 18 '17 at 13:37
2

Using this answer to How can I clip an image via a bezier-path? (linked by jake in the comments) and inserting example-images you get the following code.

\documentclass{standalone}
\usepackage{graphicx} %Manage images of different formats
\usepackage{tikz}   % drawing

\newif\ifdeveloppath
\tikzset{/tikz/develop clipping path/.is if=developpath,
  /tikz/develop clipping path=true}

\newcommand{\clippicture}[2]{
    \begin{tikzpicture}
        % Include the image to determine the size and set up the relative coordinate system. Enclose the \includegraphics in \phantom{} once the clipping path has been set up
        \ifdeveloppath
            \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics#1};
        \else
            \node[anchor=south west,inner sep=0] (image) at (0,0) {\phantom{\includegraphics#1}};
        \fi
        \pgfresetboundingbox
        \begin{scope}[x={(image.south east)},y={(image.north west)}]
            % Draw grid while developing clipping path
            \ifdeveloppath
                \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
                \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
                \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
                \draw[red, ultra thick] #2 -- cycle;
            \else
                % Use the path to clip, include the image
                \path[clip] #2 -- cycle;
                \node[anchor=south west,inner sep=0pt] {\includegraphics#1};
            \fi
        \end{scope}
    \end{tikzpicture}
}

\begin{document}

\tikzset{develop clipping path=false}

\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.9\textwidth]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[->] (0.014,0) -- (1,0);
        \draw (1,0) node[anchor= west]{$N$};
        \draw[->] (0,0.02) -- (0,1);
        \draw (0,1) node[anchor=north west]{$Z$};
        \draw[dashed] (0.014,0.02) -- (0.6388,0.95) node[anchor=north west]{$N=Z$}; 
        \node[anchor=south west] at (0.01,0.4){\clippicture{[width=0.35\textwidth]{example-image-b}}{(0.55,0) -- (1,0.9) -- (1,1) -- (0.1,1) -- (0,0.3) -- (0,0)}}; % path to clip
        \draw[thick,dashed] (0.2,0.35) -- (0.45,0.85);
        \draw[thick,dashed] (0.005,0.45) -- (0.08,0.98);
    \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

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.