无法使用 tikz-cd 环境定义命令(函子符号)

无法使用 tikz-cd 环境定义命令(函子符号)

我已经讲得太多了,所以简短的问题是:

为什么我不能用tikzcd下面定义的包定义宏?(就在“旧解决方案”部分上方)


描述

我最近开发了一种函子符号,使用环境很诱人tikzcd。但是,问题是 1)宏必须有超过 9 个条目(我找到了一个解决方案这里)。2)间距非常糟糕。

我找到了一个使用数组和一些旋转的解决方案(解决方案代码将在最后),但大多数情况下它运行良好。

旧解决方案输出

但是,当使用大类别名称或其他名称时,术语之间的空格会变得非常奇怪和蹩脚。回到tikzcd,我几乎用了一些额外的技巧(但主要是“更公平”):

\begin{tikzcd}[row sep={0.22em}, column sep={0.01em}]
    {F}: & {\boldsymbol{\mathscr C}} \arrow[rr] &\hspace{0.5cm}& {\boldsymbol{\mathscr D}}        &   \\
    & {X} \arrow[dd,"{\phi}"'] \arrow[rr, mapsto] & & {F(X)} \arrow[dd,"{F(\phi)}"'] & {x} \arrow[dd,mapsto]\\
    & \,                                         & &              &   \\ 
    & {Y} \arrow[rr,mapsto]                     & & {Y}         & {F(\phi)(x)}
\end{tikzcd}

其结果为:

在此处输入图片描述

现在箭头会根据每种情况进行调整。所以我完成了,并想定义该宏。复制我以前的做法,应该是:

%----------------
% Black Tex Magic
%----------------
% Provide a way to declare and renew a command in one command
\newcommand{\neworrenewcommand}[1]{\providecommand{#1}{}\renewcommand{#1}}

% Covariant functor
\newcommand{\covfunctor}[9]{
    \neworrenewcommand{\ccovfunctor}[2]{
        \begin{tikzcd}[row sep={0.22em}, column sep={0.01em}]
            {#1}: & {#2} \arrow[rr]                            &\hspace{0.5cm} & {#3}        &   \\
            & {#4} \arrow[dd,"{#8}"'] \arrow[rr, mapsto] & & {#5} \arrow[dd,"{#9}"'] & {##1} \arrow[dd,mapsto]\\
            & \,                                         & &              &   \\ 
            & {#6} \arrow[rr,mapsto]                     & & {#7}         & {##2}
        \end{tikzcd}
    }
    \ccovfunctor
}

嗯... TeX 中没有这么简单的事情:它编译时出现错误,但并未显示错误的具体内容。那么... 哪里出错了?

旧解决方案

其用法与上面的相同\neworrenewcommand

\usepackage{rotating}
\usepackage{scalerel}
\newcommand{\covfunctor}[9]{
    \neworrenewcommand{\ccovfunctor}[2]{
        \begin{array}{rcccc}
            {#1}: & \hphantom{{#8}} {#2} & \longrightarrow & {#3} &  \\
            &   \hphantom{{#8}} {#4}    &   \longmapsto   & {#5} & {##1}\\
            &  \begin{turn}{90}
                \ensuremath{\overset{\begin{turn}{270}
                            \scaleobj{0.7}{\ensuremath{{#8}}}
                    \end{turn}}{\longleftarrow}}
            \end{turn} &  & \begin{turn}{90}
                \ensuremath{\overset{\begin{turn}{270}
                            \scaleobj{0.7}{\ensuremath{{#9}}}
                    \end{turn}}{\longleftarrow}}
            \end{turn} \hphantom{\scaleobj{0.7}{{#9}}} & \begin{turn}{90}
                \reflectbox{\ensuremath{\longmapsto}}
            \end{turn}\\
            &  \hphantom{{#8}} {#6} & \longmapsto & {#7} & {##2}
        \end{array}
    }
    \ccovfunctor
}

答案1

我相信所有内容都在您发布的链接和代码中。请注意,这&\hspace{0.5cm}&是不对的,您应该写&[1cm]&,并且不需要额外的行,这个空间可以用 调整\\[5mm]

\documentclass{article}
\usepackage{mathtools}
\usepackage{mathrsfs}
\usepackage{tikz-cd}

%This is a first attemp with only 7 arguments
\newcommand{\funtordia}[7]{%
  \begin{tikzcd}[row sep={0.22em}, column sep={0.01em}, ampersand replacement=\&]
    #1: \& \mathscr{#2} \arrow[rr] \&[1cm]\& \mathscr{#3} \&   \\
    \& #5 \arrow[d,"#4"'] \arrow[rr, mapsto] \& \& #1(#5) \arrow[d,"#1(#4)"'] \& #7 \arrow[d,mapsto]\\[5mm]
    \& #6 \arrow[rr,mapsto]                     \& \& #1(#6)         \& #7\cdot #4
  \end{tikzcd}
}

%This is taken from the egreg's answer in the link you posted
\ExplSyntaxOn
\NewDocumentCommand{\NewWeirdCommand}{mm}
 {% #1 = command to define, #2 = replacement text
  \cs_new:Npn #1 ##1
   {
    \tl_set:Nn \l__covfun_args_tl { ##1 }
    #2
   }
 }
\NewDocumentCommand{\Arg}{m}
 {
  \tl_item:Nn \l__covfun_args_tl { #1 }
 }

\tl_new:N \l__covfun_parse_args_tl
\ExplSyntaxOff

\NewWeirdCommand{\covfunctor}{%
  \begin{tikzcd}[row sep={0.22em}, column sep={0.01em}, ampersand replacement=\&]
    \Arg{1}: \& \mathscr{\Arg{2}} \arrow[rr] \&[1cm]\& \mathscr{\Arg{3}} \&   \\
    \& \Arg{5} \arrow[d,"\Arg{4}"'] \arrow[rr, mapsto] \& \& \Arg{8} \arrow[d,"\Arg{7}"'] \& \Arg{10} \arrow[d,mapsto]\\[5mm]
    \& \Arg{6} \arrow[rr,mapsto]                     \& \& \Arg{9}         \& \Arg{11}
  \end{tikzcd}
}

\begin{document}

\funtordia{G}{D}{C}{d}{D}{D'}{y}%
\qquad%
\covfunctor{{F}{A}{B}{a}{A}{A'}{b}{B}{B'}{x}{y}}

\end{document}

生成以下内容 在此处输入图片描述

答案2

使用 11 个参数很容易出错。我建议采用不同的方法,使用更自然的语法“按列”。这个想法是存储列数据,并在图表中需要时选择所需的项目。

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

\ExplSyntaxOn

% the syntax will be by columns:
% \describefunctor{F}{\mathcal{C},X,\phi,Y}{\mathcal{D},F(X),F(\phi),F(Y)}[x,F(\phi)(x)]
% with the last column optional

\NewDocumentCommand{\describefunctor}{mmmo}
 {
  \IfNoValueTF{#4}
   {
    \bool_set_false:N \l__mezzovilla_three_bool
    \mezzovilla_functor:nnnn { #1 } { #2 } { #3 } { }
   }
   {
    \bool_set_true:N \l__mezzovilla_three_bool
    \mezzovilla_functor:nnnn { #1 } { #2 } { #3 } { #4 }
   }
 }

\clist_new:N \l__mezzovilla_functor_one_clist
\clist_new:N \l__mezzovilla_functor_two_clist
\clist_new:N \l__mezzovilla_functor_three_clist
\bool_new:N \l__mezzovilla_three_bool

\cs_new_protected:Nn \mezzovilla_functor:nnnn
 {
  \clist_set:Nn \l__mezzovilla_functor_one_clist { #2 }
  \clist_set:Nn \l__mezzovilla_functor_two_clist { #3 }
  \clist_set:Nn \l__mezzovilla_functor_three_clist { #4 }
  \begin{tikzcd}[ampersand~replacement=\&]
  #1\colon \&[-3em]
  \__mezzovilla_item:nn { one } { 1 } \arrow[r] \&
  \__mezzovilla_item:nn { two } { 1 }
  \bool_if:NT \l__mezzovilla_three_bool { \&[-1.5em] }
  \\[-1.5em]
  \& \__mezzovilla_item:nn { one } { 2 }
     \arrow[r,mapsto]
     \arrow[d,swap,"\__mezzovilla_item:nn { one } { 3 }"]
  \& \__mezzovilla_item:nn { two } { 2 }
     \arrow[d,swap,"\__mezzovilla_item:nn { two } { 3 }"]
  \bool_if:NT \l__mezzovilla_three_bool
   { \& \__mezzovilla_item:nn { three } { 1 } \arrow[d,mapsto] }
  \\
  \& \__mezzovilla_item:nn { one } { 3 }
     \arrow[r,mapsto]
  \& \__mezzovilla_item:nn { two } { 3 }
  \bool_if:NT \l__mezzovilla_three_bool
   { \& \__mezzovilla_item:nn { three } { 2 } }
  \end{tikzcd}
 }

\cs_new:Nn \__mezzovilla_item:nn
 {
  \clist_item:cn { l__mezzovilla_functor_#1_clist } { #2 }
 }

\ExplSyntaxOff

\begin{document}

\[
\describefunctor{F}{\mathcal{C},X,\phi,Y}{\mathcal{D},F(X),F(\phi),F(Y)}[x,F(\phi)(x)]
\]

\[
\describefunctor{F}{\mathcal{C},X,\phi,Y}{\mathcal{D},F(X),F(\phi),F(Y)}
\]

\end{document}

在此处输入图片描述

相关内容