2

I like to draw commutative diagrams using the xymatrix command. What I do not like is the fact that xymatrix is a command and not an environment. The structure of the xymatrix minilanguage is similar to some LaTeX environments that provide alignment of math formulas (array,align etc.), and it feels just weird to use a command in the xymatrix case.

I would like to define an environment that behaves exactly like the xymatrix command, but I do not know enough TeX to do that, so my naive attempts fail.

Could you write such an environment, or point me towards solution of my problem?

  • 1
    I've posted an answer using \xymatrix, but perhaps take a look at https://tex.stackexchange.com/questions/115783/how-to-draw-commutative-diagrams as tikz-cd is likely the best plan. – Joseph Wright Jan 09 '23 at 10:26

1 Answers1

3

You could use the b argument type in \NewDocumentEnviornment to grab all of the content then pass to \xymatrix. For example

\documentclass{article}
\usepackage[all,cmtip]{xy}

\NewDocumentEnvironment{xyenv}{ob}{ \IfNoValueTF{#1}{\xymatrix{#2}}{\xymatrix@#1{#2}}% }{}

\begin{document}

\xymatrix{ U \ar@/_/[ddr]_y \ar@/^/[drr]^x \ar@{.>}[dr]|-{(x,y)} \ & X \times_Z Y \ar[d]^q \ar[r]_p & X \ar[d]_f \ & Y \ar[r]^g & Z }

\begin{xyenv} U \ar@/_/[ddr]_y \ar@/^/[drr]^x \ar@{.>}[dr]|-{(x,y)} \ & X \times_Z Y \ar[d]^q \ar[r]_p & X \ar[d]_f \ & Y \ar[r]^g & Z \end{xyenv}

\xymatrix@C+1em{ U \ar@/_/[ddr]_y \ar@/^/[drr]^x \ar@{.>}[dr]|-{(x,y)} \ & X \times_Z Y \ar[d]^q \ar[r]_p & X \ar[d]_f \ & Y \ar[r]^g & Z }

\begin{xyenv}[C+1em] U \ar@/_/[ddr]_y \ar@/^/[drr]^x \ar@{.>}[dr]|-{(x,y)} \ & X \times_Z Y \ar[d]^q \ar[r]_p & X \ar[d]_f \ & Y \ar[r]^g & Z \end{xyenv}

\end{document}

(In a real use, I might be minded to save \xymatrix with some internal name then create an environment called xymatrix here: for a demo, that looked a bit more risky.)

enter image description here

egreg
  • 1,121,712
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • \xymatrix accepts some material before the { in order to set row and column spaces. Probably an argument list O{}b and doing \xymatrix#1{#2} would be better. And, of course, the option cmtip must be passed to xy or the arrows would be ugly as hell. – egreg Jan 09 '23 at 10:33
  • I'll adjust: oddly this is not something I'm familiar with! – Joseph Wright Jan 09 '23 at 10:35
  • It was a bit more complicated than I thought initially. I added an example of usage and the pictures. – egreg Jan 09 '23 at 10:41