4

I have the following diagram:

\begin{tikzcd}[ampersand replacement=\&]
   x \arrow[rr] \arrow[dr] \&  \& y \arrow[dl]  \\ 
    \& z \&   \\
\end{tikzcd}

and I would like to use:

\fill[pattern=north east lines, pattern color=blue],

to fill it with that pattern. What is the correct way to do this? I am just getting errors every time I try this.

ice1000
  • 103
Abellan
  • 761

3 Answers3

6

I could be tempted to close as duplicate of the question you referred to in the first question you posted about this: tikz-cd: Shade faces of commutative cube

This is basically the same as what is described in the last part of Gonzalo's answer, but using your \fill command instead.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{patterns,backgrounds}
\begin{document}

\begin{tikzcd}[
  ampersand replacement=\&,
  execute at end picture={
    \scoped[on background layer]
    \fill[pattern=north east lines, pattern color=blue] (a.center) -- (b.center) -- (c.center) -- cycle;
  }]
   |[alias=a]|x \arrow[rr] \arrow[dr] \&  \& |[alias=b]|y \arrow[dl]  \\ 
    \& |[alias=c]|z \&   
\end{tikzcd}
\end{document} 
Torbjørn T.
  • 206,688
3

I think it's ugly-looking, however, if this is what you want...

You can use the fit library and execute at end picture option:

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{fit, patterns}

\begin{document} [ \begin{tikzcd}[ampersand replacement=&, execute at end picture={ \node[rectangle, draw, blue, pattern=north east lines, pattern color=blue, fit={(tikz@f@1-1-1) (tikz@f@1-1-3) (tikz@f@1-2-2)} ] {}; }
] x \arrow[rr] \arrow[dr] & & y \arrow[dl] \ & z & \ \end{tikzcd} ] \end{document}

enter image description here And if someone would like the pattern in the background:

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{fit, patterns, backgrounds}

\begin{document} [ \begin{tikzcd}[ampersand replacement=&, execute at end picture={ \begin{scope}[on background layer] \node[rectangle, draw, blue, pattern=north east lines, pattern color=blue, fit={(tikz@f@1-1-1) (tikz@f@1-1-3) (tikz@f@1-2-2)} ] {};
\end{scope}}] x \arrow[rr] \arrow[dr] & & y \arrow[dl] \ & z & \ \end{tikzcd} ] \end{document}

enter image description here

Note: The original version of this answer used overlay but it is not needed and caused vartical overlay with the text:

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{fit, patterns}
\usepackage{mwe}
\begin{document}
\blindtext
    \[
    \begin{tikzcd}[remember picture, overlay, ampersand replacement=\&]
    x \arrow[rr] \arrow[dr] \&  \& y \arrow[dl]  \\ 
    \& z \&   \\
    \end{tikzcd}
    \tikz[remember picture, overlay]{
        \node[rectangle, draw, blue,
                pattern=north east lines, pattern color=blue,
                fit={(tikz@f@1-1-1) (tikz@f@1-1-3)  (tikz@f@1-2-2)}
            ] 
        {};
    }
    \]
\blindtext
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • Very nice solution with execute at end picture. Unfortunately I can't up-vote twice :-(. – Zarko Mar 08 '21 at 00:36
  • @Zarko Thank you! I realized the original solution was wrong only yesterday because someone upvoted it :) – CarLaTeX Mar 08 '21 at 06:38
2

As suplement to the first solution of the CarLaTeX answer. Small variations are:

  • style for node with pattern is defined by the \tikzset
  • for pattern is used pattern.meta packages,
  • defined is matrix name
\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{fit,
                patterns.meta,
                shapes.geometric}
\tikzset{
F/.style = {draw=blue, inner ysep=0pt, fit=#1,
            postaction={pattern={Lines[angle=45,distance={2pt},
                                       line width=0.25pt]},
                        pattern color=blue!50, semitransparent} }
        }
\usepackage{lipsum}

\begin{document} \lipsum[65] [ \begin{tikzcd}[ every matrix/.append style = {name=m}, execute at end picture = { \node[F = (m-1-1) (m-1-3) (m-2-2) ] {}; }% end of execute at end picture ] x \ar[rr] \ar[dr] & & y \ar[dl] \ & z &
\end{tikzcd} ] \lipsum[66] \end{document}

enter image description here

Zarko
  • 296,517