1

I'm trying to draw a diagram of a function that is Injective but not Surjective. I'm using standalone, and want the end result to look like the this image:

A rough idea

My code is modified from https://tex.stackexchange.com/a/167913/197489 without much luck. Here is my LaTeX

\documentclass[crop,tikz]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
    \tikz[overlay,remember picture]{
        \draw[black,#1]
        ($(#2)+(-0.5em,2.0ex)$) rectangle
        ($(#3)+(0.75em,-0.75ex)$);}
}

%Draw an arrow diagram that represents a function that is an injection but is not a surjection.

\begin{document}
        \begin{tikzcd}[scale=.1]
        \tikzmark{Atop} a \arrow[r] & \tikzmark{Btop}x \\
        b \arrow[r] & y \\
        \tikzmark{Abottom}& z\tikzmark{Bbottom} \\
        \DrawBox[thick, red]{Atop}{Abottom}
        \DrawBox[thick, blue]{Btop}{Bbottom}
        \end{tikzcd}
\end{document}

Which results in

Badly cropped tikz

Any suggestions?

I'm using MiKTeX in TeXStudio if that helps.

3
  • This nests tikzpictures, which causes the problems.
    – user194703
    Apr 28, 2020 at 18:37
  • @Schrödinger'scat My bad, I edited the question and I still have a similar problem
    – Neil_P
    Apr 28, 2020 at 18:50
  • 1
    Your (handmade) \tikzmark command starts with \tikz which means that it creates a tikzpicture.
    – user194703
    Apr 28, 2020 at 18:52

1 Answer 1

1

Your approach nests tikzpictures. This is because \tikz starts a new tikzpicture and you are using it inside tikzcd, which is a tikzpicture on its own.

To draw something that resembles the screen shot you post, tikz-cd may not even the best choice. You can just use, for example, this code:

\documentclass[crop,tikz]{standalone}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
 \matrix[matrix of nodes,nodes={circle,fill,inner sep=2pt},
    column sep=4em,row sep=1em,inner sep=0pt] (mat)
 {
  |[label=above left:$a$]| {} & |[label=above right:$x$]| {}\\
  |[label=above left:$b$]| {} & |[label=above right:$y$]| {}\\
  & |[label=above right:$z$]| {}\\
 };
 \node[blue,draw,very thick,fit={(mat.north west) ([xshift=1ex]mat-1-1.east|-mat.south)},
    inner sep=1ex,label={[blue]above:$S$}]{};
 \node[red,draw,very thick,fit={(mat.north east) ([xshift=-1ex]mat-1-2.west|-mat.south)},
 inner sep=1ex,label={[red]above:$T$}]{};
 \foreach \X in {1,2}
 {\draw[-stealth] (mat-\X-1) -- (mat-\X-2);}
\end{tikzpicture}
\end{document}

enter image description here

Notice that you can safely draw something in an tikzcd environment by using the execute at end picture hook.

\documentclass[crop,tikz]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzcd}[column sep=4em,
    execute at end picture={%
 \node[blue,draw,very thick,fit={(\tikzcdmatrixname.north west) ([xshift=1ex]\tikzcdmatrixname-1-1.east|-\tikzcdmatrixname.south)},
    inner sep=1ex,label={[blue]above:$S$}]{};
 \node[red,draw,very thick,fit={(\tikzcdmatrixname.north east) ([xshift=-1ex]\tikzcdmatrixname-1-2.west|-\tikzcdmatrixname.south)},
 inner sep=1ex,label={[red]above:$T$}]{};
    }]
         a \arrow[r] & x \\
        b \arrow[r] & y \\
        & z 
\end{tikzcd}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .