3

About this code, I'd like to create two boxes around these tikz-cds. Is it possible? Thank you so much. The code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage{tikz}
\usepackage{tikz-cd}
\usepackage{stix}
\usepackage[bb=ams]{mathalpha}
\usepackage{mathtools}

\newcommand{\tonde}[1]{\bigl(#1\bigr)}

\newcommand{\R}{\mathbb R} \newcommand{\A}{\mathbb A}

\title{TikZ - tikzsets} \author{PUCK} \date{November 2022}

\begin{document}

\maketitle

\Large

\section{Introduction}

\begin{equation} \begin{tikzcd}[arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, row sep=1.25cm,column sep=2cm] M\arrow[r, "f\in C^\infty"] & N \ g\tonde{U} \arrow[u, hook, "i_M"] \arrow[r, "\bar{f}\in C^\infty"] & h\tonde{V}\arrow[u, hook, "i_N"']\U\arrow[u, hook, two heads, "g", "\eqqsim"'] \arrow[r, hook, two heads, "\phi\coloneqq h^{-1}\circ f\circ g","\eqqsim"'] &V\arrow[u, hook, "h"', "\eqqsim"] \end{tikzcd}\implies \begin{tikzcd}[arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, row sep=1.5cm, column sep=2cm] \textbf{T}_xM\subset\R^k\arrow[r, "df_x"] & \textbf{T}_yN \subset\R^l\\textbf{T}_uU=\R\A^m\arrow[r, "d\phi_u"'] \arrow[u, "dg_u"] & \textbf{T}_vV=\R\A^n \arrow[u, "dh_v"'] \end{tikzcd} \end{equation}

\end{document}

Box Tikz-cd

Puck
  • 1,208
  • I have read both answers and liked them. But I don't know which one is the best. Let me explain: I chose the Qrrbrbirlbel's version because it is more familiar to me, unfortunately I do not know the preview-environment used in the Zarko's version. I can accept any other answer. I thank you all. – Puck Nov 04 '22 at 14:14
  • The preview package/environment is only there to extract a nice picture for the answer. It's not necessary for the answer. (But I'd argue its to convulated in the present form.) CarLaTeX is a great solution (but see my comment there) but will not take arrows or labels in consideration that go outside the matrix's boundary. My answer will and could be made without the backgrounds library unless you want to fill the background of your diagram. They can all made into a short-named style that gets applied to the tikzcd environment or the every diagram style. – Qrrbrbirlbel Nov 04 '22 at 15:29

3 Answers3

4

Like this?

enter image description here

Edit:
Deleted is use of preview package which is used to see only equation with tikzcd diagrams. It seems that this trick is not known to OP, so it is better to remove it. Now compilation result is seen on top of whole page. Hopefully that now proposed solutions are more clear.

Rectangles are draw as fit nodes in execute at end picture:

\documentclass{article}
\usepackage{stix}
\usepackage[bb=ams]{mathalpha}
\usepackage{mathtools}
\newcommand{\R}{\mathbb R}
\newcommand{\A}{\mathbb A}

\usepackage{tikz-cd} \usetikzlibrary{arrows.meta, fit, patterns.meta} \tikzcdset{ arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, } \tikzset{ every matrix/.append style = {name=m}, F/.style = {draw=red, very thick, rounded corners, inner sep=5pt, fit=#1} }

\begin{document} \begin{equation} \begin{tikzcd}[column sep=24mm, row sep=12mm, execute at end picture = { % <---- \node[F = (m-1-1) (m-3-2) ] {}; }% end of execute at end picture ] M \rar["f\in C^\infty"] & N \ g(U) \uar[hook, "i_M"] \rar["\bar{f}\in C^\infty"] & h(V)\uar[hook, "i_N"'] \ U \uar[hook, two heads, "g", "\eqqsim"'] \rar[hook, two heads, "\phi\coloneqq h^{-1}\circ f\circ g","\eqqsim"'] & V \uar[hook, "h"', "\eqqsim"] \end{tikzcd} \implies \begin{tikzcd}[sep=12mm, execute at end picture = {% <---- \node[F = (m-1-1) (m-2-2) ] {}; }% end of execute at end picture ] \textbf{T}_xM\subset\R^k \rar["df_x"] & \textbf{T}_yN \subset\R^l \ \textbf{T}_uU=\R\A^m \rar["d\phi_u"'] \uar["dg_u"] & \textbf{T}_vV=\R\A^n \uar["dh_v"'] \end{tikzcd} \end{equation} \end{document}

Addendum:

  • Without defining common \tikzset for node style. Style is now defined in node.
  • Naming of matrix by every matrix/.append style = {name=m} is replaced by use of tikz@f@... for diagram name:
\documentclass{article}
\usepackage{stix}
\usepackage[bb=ams]{mathalpha}
\usepackage{mathtools}
\newcommand{\R}{\mathbb R}
\newcommand{\A}{\mathbb A}

\usepackage{tikz-cd} \usetikzlibrary{arrows.meta, fit, patterns.meta} \tikzcdset{ arrow style=tikz, >={Triangle[length=2mm]}, }

\begin{document} \begin{preview} \begin{equation} \begin{tikzcd}[column sep=24mm, row sep=12mm, execute at end picture = { % <---- \node[draw=red, very thick, rounded corners, fit=(tikz@f@1)] {}; % first diagram }% end of execute at end picture ] M \rar["f\in C^\infty"] & N \ g(U) \uar[hook, "i_M"] \rar["\bar{f}\in C^\infty"] & h(V)\uar[hook, "i_N"'] \ U \uar[hook, two heads, "g", "\eqqsim"'] \rar[hook, two heads, "\phi\coloneqq h^{-1}\circ f\circ g","\eqqsim"'] & V \uar[hook, "h"', "\eqqsim"] \end{tikzcd} \implies \begin{tikzcd}[sep=12mm, execute at end picture = {% <---- \node[draw=red, very thick, rounded corners, fit=(tikz@f@2)] {}; % second diagram }% end of execute at end picture ] \mathbf{T}_xM\subset\R^k \rar["df_x"] & \mathbf{T}_yN \subset\R^l \ \mathbf{T}_uU=\R\A^m \rar["d\phi_u"'] \uar["dg_u"] & \mathbf{T}_vV=\R\A^n \uar["dh_v"'] \end{tikzcd} \end{equation} \end{preview} \end{document}

  • As suggested @Qrrbrbirlbel in his comment, name of diagram as \tikzcdmatrixname is available at the end of the diagram (when execute at end picture is executed. So nodes code can be changed to:
...
    \begin{tikzcd}[...,
execute at end picture = {% <---- at each tikzcd diagram
    \node[draw=red, very thick, rounded corners, fit=(\tikzcdmatrixname)] {};
                         }% end of execute at end picture
...

Compilation result is similar as before:

enter image description here

Zarko
  • 296,517
  • Ok thank you. In my real latex document (I'm writing a book in Differential Geometry) I was forced to define several tikzsets, is there a simplier way to obtain what I desire without define all of these tikzsets? Thank you – Puck Nov 04 '22 at 11:50
  • @Puck, what is for you a "simpler way"? You can move style definitions into equation or in tikzcd, if you wish (if this is simpler to you?). BTW, instead of saying "thank you" you can upvote answer (at least). It solve your question ... – Zarko Nov 04 '22 at 12:03
  • Instead of naming the matrix m you can use \tikzcdmatrixname that is available at the end of the diagram (when execute at end picture gets executed). But you could then just say fit=(\tikzcdmatrixname) (which would then also include the inner seps of the matrix). – Qrrbrbirlbel Nov 04 '22 at 15:22
  • @Qrrbrbirlbelm I know. I just more prefer named diagram nodes on showed way. But, since OP complain about complexity of answer, maybe (s)he will more liked answer using your suggestion :-). Thank you for tip! And +1 for your interesting answer! – Zarko Nov 04 '22 at 16:56
4

You can use the backgrounds library library to draw a rectangular box around the whole diagram. See the manual for more options on how to change the outcome of that rectangle, mostly the settings on inner frame seps.

I've also

  • used \DeclarePairedDelimiter from mathtools to define \tonde.

    Now you can use \tonde*{<stuff>} and the parentheses are scaled automatically but you can also do \tonde[\big]{<stuff>} and \bigl(<stuff>\bigr) will be typeset. (For a single V there's no need to use \big?.)

  • defined a my CD style for repeated settings for your diagram. If you want all your diagrams to have these options you can just append these options to every diagram:

    \tikzcdset{every diagram/.append style={…}}
    
  • used \mathbf instead of \textbf.

  • and defined a \diff for the differential d (and commented out the version for an upright d).

The amsmath package also provides a \boxed macro that could be used here but doesn't the whole functionality that TikZ brings.

Code

\documentclass{article}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage{tikz-cd}
\usepackage{stix}
\usepackage[bb=ams]{mathalpha}
\usepackage{mathtools}
\newcommand{\R}{\mathbb R}
\newcommand{\A}{\mathbb A}

\DeclarePairedDelimiter{\tonde}() % better then your own \tonde \newcommand{\diff}{d} % diff d italic %\newcommand{\diff}{\mathup{d}} % diff d upright \usetikzlibrary{backgrounds} % for background rectangle \tikzcdset{ my CD/.style={ % shortcut for repeated style arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, row sep=1.25cm, column sep=2cm}} \tikzset{ boxed/.style={
show background rectangle, % adds a box around the whole picture background rectangle/.append style={ % that is red, thick and rounded draw=red, thick, rounded corners}}} \begin{document} \Large \section{Introduction}

\begin{equation} \begin{tikzcd}[my CD, boxed] M \arrow[r, "f\in C^\infty"] & N \ g\tonde{U} \arrow[u, hook, "i_M"] \arrow[r, "\bar{f}\in C^\infty"] & h\tonde{V} \arrow[u, hook, "i_N"'] \ U \arrow[u, hook, two heads, "g", "\eqqsim"'] \arrow[r, hook, two heads, "\phi\coloneqq h^{-1}\circ f\circ g", "\eqqsim"'] & V \arrow[u, hook, "h"', "\eqqsim"] \end{tikzcd} \implies \begin{tikzcd}[my CD, boxed] \mathbf{T}_xM \subset\R^k \arrow[r, "\diff f_x" ] & \mathbf{T}_yN \subset\R^l \ \mathbf{T}_uU=\R\A^m \arrow[r, "\diff \phi_u"'] \arrow[u, "\diff g_u" ] & \mathbf{T}_vV=\R\A^n \arrow[u, "\diff h_v"' ] \end{tikzcd} \end{equation} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • The OP uses an italic d, I don't see any reason to change their style. – egreg Nov 04 '22 at 13:05
  • 2
    @egreg I don't like it that much but these diagrams aren't for me in the first place. I still define a macro for it but OP doesn't need to follow every suggestion they from my answer. – Qrrbrbirlbel Nov 04 '22 at 13:12
4

I suggest appending a style to the matrix (the CD is a TikZ matrix), for convenience I created mymatr style (updated according to Qrrbrbirlbel's advice):

\documentclass{article}
%\usepackage[utf8]{inputenc} no more needed in up-to-date distributions: https://tex.stackexchange.com/a/370279/101651
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
%\usepackage{tikz} not needed, tikz-cd already load it
\usepackage{tikz-cd}
\tikzset{mymatr/.style={every outer matrix/.append style={draw=red, inner xsep=4pt , inner ysep=6pt, rounded corners, very thick}}}

\usepackage{stix} \usepackage[bb=ams]{mathalpha} \usepackage{mathtools}

\newcommand{\tonde}[1]{\bigl(#1\bigr)}

\newcommand{\R}{\mathbb R} \newcommand{\A}{\mathbb A}

\title{TikZ - tikzsets} \author{PUCK} \date{November 2022}

\begin{document}

\maketitle

\Large

\section{Introduction}

\begin{equation} \begin{tikzcd}[mymatr, arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, row sep=1.25cm,column sep=2cm] M\arrow[r, "f\in C^\infty"] & N \ g\tonde{U} \arrow[u, hook, "i_M"] \arrow[r, "\bar{f}\in C^\infty"] & h\tonde{V}\arrow[u, hook, "i_N"']\U\arrow[u, hook, two heads, "g", "\eqqsim"'] \arrow[r, hook, two heads, "\phi\coloneqq h^{-1}\circ f\circ g","\eqqsim"'] &V\arrow[u, hook, "h"', "\eqqsim"] \end{tikzcd}\implies \begin{tikzcd}[mymatr,
arrow style=tikz, >={Triangle[length=2mm]}, cells={nodes={inner sep=2mm}}, row sep=1.5cm, column sep=2cm] \textbf{T}_xM\subset\R^k\arrow[r, "df_x"] & \textbf{T}_yN \subset\R^l\\textbf{T}_uU=\R\A^m\arrow[r, "d\phi_u"'] \arrow[u, "dg_u"] & \textbf{T}_vV=\R\A^n \arrow[u, "dh_v"'] \end{tikzcd} \end{equation}

\end{document}

enter image description here

CarLaTeX
  • 62,716
  • Thank you @CarLaTeX, your solution is very good. – Puck Nov 04 '22 at 14:59
  • 1
    I'd do \tikzset{mymatr/.style={every outer matrix/.append style={draw=red, inner xsep=4pt , inner ysep=6pt, rounded corners, very thick}}} to be on the safe side. That way, the order doesn't matter and only mymatr needs to be added. – Qrrbrbirlbel Nov 04 '22 at 15:17
  • @Qrrbrbirlbel Thank you for the advice, updated. – CarLaTeX Nov 04 '22 at 20:03