3

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:

\usepackage{graphicx}
 \newcommand{\rotin}{\rotatebox[origin=c]{90}{$\in$}}
 \begin{tikzpicture}
        \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);
    \end{tikzpicture}

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.

1

2 Answers 2

3

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

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}

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

\newcommand{\function}[5]{%
  \begin{tikzcd}[
    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]
  \end{tikzcd}%
}

\begin{document}

\begin{gather*}
\function{f}{X}{Y}{x}{y} \\
\function{\Phi}{S_n}{S_n}{\sigma}{\tau\sigma\tau^{-1}}
\end{gather*}

\end{document}

enter image description here

1

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.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
 \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);
    \end{tikzpicture}
\end{document}

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 .