数组单元格突出显示 tikz

数组单元格突出显示 tikz

我正在开始撰写博士论文,对 LaTeX 还不太熟悉。我试图解释我正在使用的数值方法,在此过程中,我希望突出显示数组的某些部分以帮助解释。我突出显示的数组中的条目大小各不相同,即它们可能是整数或长度为几个字符的较长项。

我发现的第一个选项是使用\cellcolor为元素赋予块颜色的命令array。但我发现这并不能提供最好的呈现效果(我知道这听起来很挑剔,但我相信更好的呈现方式会更好)。无论如何,我发现的第二个选项是通过这里的一些示例,主要是通过定义节点和创建具有可自定义选项的矩形等。这些示例看起来不错,我想在我的工作中使用类似的东西,但是,现在,所有示例都适用于简单数组,即充满整数的数组,但对于我正在使用的数组来说效果不太好。

您会从我上传的代码中注意到,矩形拟合直接适合数组单元格内的文本,这会导致两个突出显示区域由于每个突出显示的节点放置/拟合而错位。\cellcolor但是,该命令适合整个“单元格”,而不是单元格内的文本。

我想突出显示数组单元格周围的方框区域而不是文本。

这将允许突出显示任何大小的数组单元,而无需对奇数大小的数组单元进行复杂的拟合。

最后,我对 LaTeX 还很陌生,在用 LaTeX 绘图方面更是“新手”。如果我没有正确解释等等,我深表歉意,请告诉我,我会尽力编辑我的回复/问题。

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{hyperref}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage{tikz}
\usetikzlibrary{calc,fit}
\tikzset{%
  highlight1/.style={rectangle,rounded corners,color=red!,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\tikzset{%
  highlight2/.style={rectangle,rounded corners,color=green!,fill=green!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline=(#1.base)] \node (#1) {#2};}
%
\newcommand{\RHighlight}[1][submatrix]{%
    \tikz[overlay,remember picture]{
    \node[highlight1,fit=(left.north west) (right.south east)] (#1) {};}
}
\newcommand{\GHighlight}[1][submatrix]{%
    \tikz[overlay,remember picture]{
    \node[highlight2,fit=(left.north west) (right.south east)] (#1) {};}
}
% Tikz drawing settings====================

\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 &  0 & -T^{1}_{12} \\
\cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\
\cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\
\cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

Using an example taken from the website, note the mis-alignment and errors of highlighted regions crossing the brackets

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\tikzmark{left}{$-T^{1}_{11}$} & 0 &  0 & -T^{1}_{12} \\
-T^{2}_{12} & \tikzmark{right}{$-T^{2}_{11}$} & 0 & 0 \GHighlight \\
\tikzmark{left}{$0$} & -T^{3}_{12} & T^{3}_{11} & 0 \\
0 & 0 & 0 & \tikzmark{right}{$0$} \RHighlight\qquad \\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{L}= \left[\begin{array}{ccc}
\tikzmark{left}{$(T^{1}_{11}+T^{1}_{12})$} & 0 & 0 \\
0 & (T^{2}_{11}+T^{2}_{12}) & \tikzmark{right}{$0$} \GHighlight \\ 
\tikzmark{left}{$0$} & 0 & (T^{3}_{11}+T^{3}_{12}) \\
 0 & 0 & \tikzmark{right}{$0$} \RHighlight\qquad \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BLphii}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{R}= \left[\begin{array}{cccc}
\tikzmark{left}{$T^{2}_{22}$} & T^{2}_{21} & 0 & 0 \\
0 & \tikzmark{right}{$T^{3}_{22}$} & T^{3}_{21} & 0 \GHighlight \\ 
\tikzmark{left}{$0$} &  0 &  0 &  0 \\
T^{1}_{21} & 0 & 0 & \tikzmark{right}{$T^{1}_{22}$} \RHighlight\qquad \\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ARphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{R}= \left[\begin{array}{ccc}
\tikzmark{left}{$0$} & -(T^{2}_{21}+T^{2}_{22}) & 0 \\
0 &  0 & \tikzmark{right}{$-(T^{3}_{21}+T^{3}_{22})$} \GHighlight \\ 
\tikzmark{left}{$0$} & 0 & 0 \\
-(T^{1}_{21}+T^{1}_{22}) & 0 & \tikzmark{right}{$0$} \RHighlight\qquad \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BRphii}
\end{equation}
\end{document}

预期结果 取得不好的结果

答案1

这是我能想到的最好的办法:使用 tikz 矩阵创建一个matrix of math nodes(您可以将其包含在数学环境中并用括号分隔),然后使用节点的隐式命名来引用矩阵的各个单元,例如:m-1-1.north east引用第一个元素的东北角。

为了避免对齐问题,您必须通过提供minimum widthandminimum height选项来确保该矩阵的所有节点具有相同的尺寸。我对这个解决方案不是很满意,因为它要求您知道较大单元的尺寸。但是,通过反复试验找到合适的值并不难。

经过几次尝试,我的代码如下:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{inputenc}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{matrix}
\usetikzlibrary{calc,fit}
\tikzset{%
  highlight1/.style={rectangle,rounded corners,color=red!,fill=red!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\tikzset{%
  highlight2/.style={rectangle,rounded corners,color=green!,fill=green!15,draw,fill opacity=0.5,thick,inner sep=0pt}
}
\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=
\begin{tikzpicture}[baseline=(m.center)]
    \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
     row sep=1mm, nodes={minimum width=3em, minimum height=1.6em}] {
      -T^{1}_{11} & 0 &  0 & -T^{1}_{12} \\
      -T^{2}_{12} &  -T^{2}_{11} & 0 & 0  \\
        0 & -T^{3}_{12} & |(r)| T^{3}_{11} & 0 \\
        0 & 0 & 0 & 0 \\
      }; 
      \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
      \node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {};
\end{tikzpicture} 
\left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

\begin{equation}\renewcommand{\arraystretch}{1.5}
B_{L}= 
\begin{tikzpicture}[baseline=(m.center)]
    \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
       row sep=1mm, nodes={minimum width=5.5em, minimum height=1.6em}] {
        (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\
        0 & (T^{2}_{11}+T^{2}_{12}) & 0 \\ 
        0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\
        0 & 0 & 0 \\
        };
    \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
    \node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BLphii}
\end{equation}


\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{R}= 
\begin{tikzpicture}[baseline=(m.center)]
    \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
      row sep=1mm, nodes={minimum width=2.5em, minimum height=1.6em}] {
        T^{2}_{22} & T^{2}_{21} & 0 & 0 \\
        0 & T^{3}_{22} & T^{3}_{21} & 0 \\ 
        0 &  0 &  0 &  0 \\
        T^{1}_{21} & 0 & 0 & T^{1}_{22}\\
        };
    \node[highlight2, fit=(m-1-1.north west) (m-2-2.south east)] {};
    \node[highlight1, fit=(m-3-1.north west) (m-4-4.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ARphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{R}= 
\begin{tikzpicture}[baseline=(m.center)]
    \matrix (m) [matrix of math nodes, left delimiter={[}, right delimiter={]},
       row sep=1mm, nodes={minimum width=6.5em, minimum height=1.6em}] {
        0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\
        0 &  0 & -(T^{3}_{21}+T^{3}_{22})\\ 
        0 & 0 & 0 \\
        -(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \\
        };
    \node[highlight2, fit=(m-1-1.north west) (m-2-3.south east)] {};
    \node[highlight1, fit=(m-3-1.north west) (m-4-3.south east)] {};
\end{tikzpicture}
\left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BRphii}
\end{equation}
\end{document}

输出结果如下:

输出

答案2

另一个解决方案可能是使用hf-tikz包:它基于\tikzmark宏,但具有可扩展标记,也就是说,要突出显示的区域的极值可以由用户定义,从而可以扩展。该解决方案已用于tikzmark 宏的问题和潜力:动态框自适应。这种方法的优点是,您不必依赖内部TikZ matrix,因此您不需要更改代码,而只需插入标记,然后扩展它。扩大说实话,这种方法的缺点显而易见:找到正确的坐标可能不是立即的,所以你应该继续进行一些试验,但毕竟,当一个人熟悉时,只需要两三次试验。还要注意,在第二个例子中,一旦确定了正确的值,就可以重复这些值(以高度为参考)。

代码:

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage[customcolors]{hf-tikz}
\newcommand{\usegreen}{
\hfsetbordercolor{green!50!black}
\hfsetfillcolor{green!20}
}
\newcommand{\usered}{
\hfsetbordercolor{red!50!black}
\hfsetfillcolor{red!20}
}
% Tikz drawing settings====================

\usepackage{hyperref} % hyperref should be loaded at end preamble

\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 &  0 & -T^{1}_{12} \\
\cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\
\cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\
\cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

Using an example taken from the website, note the mis-alignment and errors of highlighted regions crossing the brackets

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\usegreen\tikzmarkin{a}-T^{1}_{11} & 0 &  0 & -T^{1}_{12} \\
-T^{2}_{12} & -T^{2}_{11}\tikzmarkend{a} & 0 & 0 \\
0 & \usered\tikzmarkin{b}-T^{3}_{12} & T^{3}_{11} & 0 \\
0 & 0 & 0 & 0 \tikzmarkend{b}\\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{L}= \left[\begin{array}{ccc}
\usegreen\tikzmarkin{c} (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\
0 & (T^{2}_{11}+T^{2}_{12})\tikzmarkend{c} & 0 \\ 
\usered\tikzmarkin{d}(0.85,-0.2)(-0.1,0.35) 0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\
 0 & 0 & 0\tikzmarkend{d} \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BLphii}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{R}= \left[\begin{array}{cccc}
\usegreen\tikzmarkin{e} T^{2}_{22} & T^{2}_{21} & 0 & 0 \\
0 & T^{3}_{22}\tikzmarkend{e} & T^{3}_{21} & 0 \\ 
\usered\tikzmarkin{f}(0.1,-0.2)(-0.25,0.35) 0 &  0 &  0 &  0 \\
T^{1}_{21} & 0 & 0 & T^{1}_{22} \tikzmarkend{f}\\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ARphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{R}= \left[\begin{array}{ccc}
\usegreen\tikzmarkin{g} 0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\
0 &  0 & -(T^{3}_{21}+T^{3}_{22}) \tikzmarkend{g} \\ 
\usered\tikzmarkin{h}(0.15,-0.2)(-0.95,0.35) 0 & 0 & 0 \\
-(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \tikzmarkend{h} \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BRphii}
\end{equation}
\end{document}

结果:

在此处输入图片描述

当然,您可以自定义突出显示以准确重现 JLDiaz 的结果:只需根据需要扩展区域即可。另一个示例:

\documentclass[a4paper,10pt,english,table]{article}

%Document normal packages
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{epstopdf}
\usepackage[parfill]{parskip}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{xcolor}

% Tikz drawing settings====================
\usepackage[customcolors]{hf-tikz}

% command to typeset the highlighted area in green
\newcommand{\usegreen}{
\hfsetbordercolor{green!75!black}
\hfsetfillcolor{green!10}
}
% command to typeset the highlighted area in red
\newcommand{\usered}{
\hfsetbordercolor{red!75!black}
\hfsetfillcolor{red!10}
}
% Tikz drawing settings====================

\usepackage{hyperref}
\begin{document}

Using 'cellcolor' command, note the array cells are highlighted and the two highlighted regions are nicely aligned.

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\cellcolor{green}-T^{1}_{11} & \cellcolor{green}0 &  0 & -T^{1}_{12} \\
\cellcolor{green}-T^{2}_{12} & \cellcolor{green}-T^{2}_{11} & 0 & 0 \\
\cellcolor{red}0 & \cellcolor{red}-T^{3}_{12} & \cellcolor{red}T^{3}_{11} & \cellcolor{red}0 \\
\cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 & \cellcolor{red}0 \\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphifcellcol}
\end{equation}

Using the \textsf{hf-tikz} package the mis-alignment and errors of highlighted regions crossing the brackets could be avoided by properly selecting/extended the highlighted area.

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{L}=\left[\begin{array}{cccc}
\usegreen\tikzmarkin{a}-T^{1}_{11} & 0 &  0 & -T^{1}_{12} \\
-T^{2}_{12} & -T^{2}_{11}\tikzmarkend{a} & 0 & 0 \\
0 & \usered\tikzmarkin{b}-T^{3}_{12} & T^{3}_{11} & 0 \\
0 & 0 & 0 & 0 \tikzmarkend{b}\\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ALphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{L}= \left[\begin{array}{ccc}
\usegreen\tikzmarkin{c} (T^{1}_{11}+T^{1}_{12}) & 0 & 0 \\
0 & (T^{2}_{11}+T^{2}_{12})\tikzmarkend{c} & 0 \\ 
\usered\tikzmarkin{d}(0.85,-0.2)(-0.85,0.375) 0 & 0 & (T^{3}_{11}+T^{3}_{12}) \\
 0 & 0 & 0\tikzmarkend{d} \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BLphii}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
 A_{R}= \left[\begin{array}{cccc}
\usegreen\tikzmarkin{e} T^{2}_{22} & T^{2}_{21} & 0 & 0 \\
0 & T^{3}_{22}\tikzmarkend{e} & T^{3}_{21} & 0 \\ 
\usered\tikzmarkin{f}(0.1,-0.2)(-0.25,0.375) 0 &  0 &  0 &  0 \\
T^{1}_{21} & 0 & 0 & T^{1}_{22} \tikzmarkend{f}\\
\end{array}\right] \left[\begin{array}{c}
\phi_{A} \\
\phi_{B} \\
\phi_{C} \\
\phi_{D}
\end{array}\right]
\label{eq:ARphif}
\end{equation}

\begin{equation}
\renewcommand{\arraystretch}{1.5}
B_{R}= \left[\begin{array}{ccc}
\usegreen\tikzmarkin{g}(0.15,-0.2)(-0.95,0.375) 0 & -(T^{2}_{21}+T^{2}_{22}) & 0 \\
0 &  0 & -(T^{3}_{21}+T^{3}_{22}) \tikzmarkend{g} \\ 
\usered\tikzmarkin{h}(1.05,-0.2)(-0.95,0.375) 0 & 0 & 0 \\
-(T^{1}_{21}+T^{1}_{22}) & 0 & 0 \tikzmarkend{h} \\
\end{array}\right] \left[\begin{array}{c}
\phi_{1} \\
\phi_{2} \\
\phi_{3}
\end{array}\right]
\label{eq:BRphii}
\end{equation}
\end{document}

结果:

在此处输入图片描述

答案3

与。{bNiceMatrix}nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\[\renewcommand{\arraystretch}{1.4}
A_L = 
\begin{bNiceMatrix}[margin]
\Block[tikz={offset=1pt,draw=green,fill=green!15,rounded corners = 2pt}]{2-2}{}
-T_{11}^1 & 0  & 0 & - T_{12}^1 \\
-T_{12}^2 & -T_{11}^2 & 0 & 0 \\
\Block[tikz={offset=1pt,draw=red,fill=red!15,rounded corners = 2pt}]{2-4}{}
0 & -T_{12}^3 & T_{11}^3 & 0 \\
0 & 0 & 0 & 0 
\end{bNiceMatrix}
\begin{bNiceMatrix}
\phi_A \\ \phi_B \\ \phi_C \\ \phi_D
\end{bNiceMatrix}\]

\end{document}

您需要进行多次编译(因为nicematrix在后台使用了 PGF/TikZ 节点)。

上述代码的输出

相关内容