I'm trying to make a command that will take in a function name, domain, codomain, arbitrary element, and what that element is mapped to, and output a diagram. The first line of the diagram will be the standard f:X-> Y. The second line will be rotated membership symbols for the domain and codomain. The third line will be something like x \mapsto f(x).

So far this is what I've tried:

        \node (functionName) at (0, 0) {f:};
        \node[right = of functionName] (domain) {A};
        \node[below = 1mm of domain] (rotinleft) {$\rotin$};
        \node[below = 1mm of rotinleft] (element) {x};
        \node[right = 1cm of domain] (codomain) {B};
        \node[below = 0.5mm of codomain] (rotinright) {$\rotin$};
        \node[below = 0.5mm of rotinright] (image) {f(x)};
        \draw[->] (domain) -- (codomain);
        \draw[|->] (element) -- (image);

I'm new to relative positioning and this doesn't look very good. I want to be able to do something like the above, but have it so the spacing and arrow lengths will automatically adjust. I never want super short arrows.


2 Answers 2


You can use code from https://tex.stackexchange.com/a/216042/4427, but in my opinion the rotated \in serves no purpose.


% from https://tex.stackexchange.com/a/216042/
    every to/.append style={
      edge node={node [sloped, allow upside down, auto=false]{$#1$}}}

    column sep=2em,
    row sep=1ex,
    ampersand replacement=\&
  #1\colon \&[-3em]
  #2\vphantom{#3} \arrow[r] \&
  #3\vphantom{#2} \\
  #4\vphantom{#5} \arrow[u,symbol=\in] \arrow[r,mapsto] \&
  #5\vphantom{#4} \arrow[u,symbol=\in]


\function{f}{X}{Y}{x}{y} \\


enter image description here


Even though I agree with @egreg that there are various alternative ways of typesetting these things, I guess your question was why the distance between the nodes was so large. This is because the predefined distance is larger than you may want it, but you can just set it to any value you like.

 \begin{tikzpicture}[node distance=1mm]
        \node (functionName) at (0, 0) {$f$:};
        \node[right = of functionName] (domain) {$A$};
        \node[below = 2mm of domain] (element) {$x$};
        \path (element)--(domain)node[midway,sloped] {$\in$};
        \node[right = 1cm of domain] (codomain) {$B$};
        \node at (element-|codomain) (image) {$f(x)$};
        \path (image)--(codomain)node[midway,sloped] {$\in$};
        \draw[->] (domain) -- (codomain);
        \draw[|->] (element) -- (image);

enter image description here

When using TikZ, there is really no need to use \rotatebox.

You must log in to answer this question.

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