tikzpicture 和 tikzlibrary 出现问题

tikzpicture 和 tikzlibrary 出现问题

我对这段代码有点困惑(在代码本身中,我用“%”注释了我想要的内容)。请帮助我。提前谢谢您。代码:

\documentclass{article}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tikz-cd}
\usepackage[customcolors,shade]{hf-tikz}
\usepackage{nicematrix}
\usetikzlibrary{calc,matrix}

\begin{document}

\begin{tikzpicture}
\edef\lstadd{{0,16,-12}} % I'd like that 0,16,-12 are expressed as $0$,$16$,$-12$ how it follows:
%\edef\lstadd{{$0$,$16$,$-12$}} How could I do?
% Moreover, I'd like that $-4$,$0$,$6$ and $0$,$16$,$-12$ are right-aligned with other 15 numbers of matrix.
\edef\lstsub{{-4,0,6}} % Here the same ...
 \matrix[matrix of math nodes,nodes={text width=1.5em}] (mat)
 { % Between III and IV columns I'd like to add a continuous (or dashed) dark-green line: How could I do?
    0 & +2 & 1 & 0 & +2 \\
    3 & -1 & 2 & 3 & -1 \\
    4 & -4 & 1 & 4 & -4 \\
 };
 \foreach \X [evaluate=\X as \Y using {int(\X+2)}]in {1,2,3}
 {\pgfmathtruncatemacro{\mylabel}{\lstadd[\X-1]}
 \draw[blue,-latex,thick] (mat-1-\X.north west) -- (mat-3-\Y.south east)
 node[pos=1.1] (LL-\X) {\mylabel};
 \pgfmathtruncatemacro{\mylabel}{\lstsub[\X-1]}
 \draw[red,-latex,thick] (mat-3-\X.south west) -- (mat-1-\Y.north east)
 node[pos=1.1] (LU-\X) {\mylabel};
 }
 \draw[line width=1mm,red,latex-,shorten >=1cm,shorten <=1cm]
 (LU-3.east) -- ++ (3,0) node[right] {Subtract these products};
 \draw[line width=1mm,blue,latex-,shorten >=1cm,shorten <=1cm]
 (LL-3.east-|LU-3.east) -- ++ (3,0) node[right] {Add these products};
\end{tikzpicture}


\end{document}

答案1

从您的代码中,我改变了几个小事情:添加backgrounds库、draw opacity嵌套scope环境等。这是您想要的吗?

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,backgrounds}
\begin{document}
\begin{tikzpicture}
\edef\lstadd{{0,16,-12}} 
\edef\lstsub{{-4,0,6}} 
\matrix[matrix of math nodes,nodes={minimum size=8mm,anchor=center}] (mat)
{
0 & +2 & 1 & 0 & +2 \\
3 & -1 & 2 & 3 & -1 \\
4 & -4 & 1 & 4 & -4 \\
};

% add [dashed] if you want
\draw[green!50!black,thick] (mat-1-3.north east)--(mat-3-3.south east);
\begin{scope}[on background layer]
\begin{scope}[line width= 5mm,draw opacity=.2,line cap=round]
\foreach \X [evaluate=\X as \Y using {int(\X+2)}]in {1,2,3}
{
\pgfmathtruncatemacro{\mylabel}{\lstadd[\X-1]}
\draw[blue] (mat-1-\X.center) -- (mat-3-\Y.center)
(mat-3-\Y.south) node[below] (LL-\X) {$\mylabel$};

\pgfmathtruncatemacro{\mylabel}{\lstsub[\X-1]}
\draw[red] (mat-3-\X.center) -- (mat-1-\Y.center)
(mat-1-\Y.north) node[above] (LU-\X) {$\mylabel$};
}
\end{scope}
\end{scope} 

\begin{scope}[line width=1mm,latex-,shorten >=5mm,shorten <=5mm]
\draw[red] (LU-3.east) -- ++ (2,0) 
node[right] {Subtract these products};
\draw[blue] (LL-3.east-|LU-3.east) -- ++ (2,0) 
node[right] {Add these products};
\end{scope}
\end{tikzpicture}
\end{document}

更新我清理代码

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes,nodes={minimum size=8mm,anchor=center}] (m)
{
0 & +2 & 1 & 0 & +2 \\
3 & -1 & 2 & 3 & -1 \\
4 & -4 & 1 & 4 & -4 \\
};
        
\draw[cyan,thick,dashed] (m-1-3.north east)--(m-3-3.south east);

\begin{scope}[line width= 5mm,draw opacity=.2,shorten >=-4mm,shorten <=-4mm]
\foreach \X/\xtext in {1/0,2/16,3/-12}{
\pgfmathsetmacro{\Y}{int(\X+2)}
\draw[blue] (m-1-\X.center) -- (m-3-\Y.center);
\path (m-3-\Y.south) node[below,blue] (LL-\X){$\xtext$};
}

\foreach \X/\xtext in {1/-4,2/0,3/6}{
\pgfmathsetmacro{\Y}{int(\X+2)}
\draw[red] (m-3-\X.center) -- (m-1-\Y.center);
\path (m-1-\Y.north) node[above,red] (LU-\X) {$\xtext$};
}
\end{scope}
        
\begin{scope}[line width=1mm,latex-,shorten >=5mm,shorten <=5mm]
\draw[blue] (LL-3.east)--+(2,0) node[right]{Subtract these products};
\draw[red] (LU-3.east)--+(2,0) node[right]{Add these products};
\end{scope}
\end{tikzpicture}
\end{document}

相关内容