是否可以改变环境内单条线的颜色array
?
我有一系列可能的解决方案,我想突出显示最好的解决方案,该数组声明为
\usepackage{amsmath}
% <...>
\begin{array}{rcc@{+}c@{+}c@{+}cl}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
&&23&0&7&5, \\
&&30&0&9&0
&\}
\end{array}
我可以使用得到一些结果
\usepackage{amsmath}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
% <...>
\begin{array}{rcc@{+}c@{+}c@{+}cl}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
&&{\color{Emerald}23}&{\color{Emerald}0}&{\color{Emerald}7}&{\color{Emerald}5}, \\
&&30&0&9&0
&\}
\end{array}
但实际上这需要太多的 TeX 命令——难道不能做得更简单吗?而且这个解决方案没有突出显示列之间的加号。我希望这些加号是分隔符,以便整个解决方案中的数字替换更加清晰。
答案1
该array
包允许在单元格开始处插入标记:
\documentclass{article}
\usepackage{array}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\begin{document}
$
\def\mycolor{}
\newcolumntype{R}{>{\mycolor}r}
\newcolumntype{C}{>{\mycolor}c}
\newcolumntype{L}{>{\mycolor}l}
\begin{array}{RCC@{\mycolor{}+{}}C@{\mycolor{}+{}}C@{\mycolor{}+{}}CL}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
\noalign{\gdef\mycolor{\color{Emerald}}} % the next row will be colored
&&23&0&7&5, \\
\noalign{\gdef\mycolor{}} % the next row won't be colored
&&30&0&9&0
&\}
\end{array}
$
\end{document}
答案2
另一个解决方案是突出显示行的背景。这里我提供了一个基于\tikzmark
宏的解决方案(基本思想取自algorithm2e + beamer 包中使用覆盖规范进行背景着色)。
代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{tikz}
\newcommand{\fcol}{Emerald!20}
\newcommand{\bcol}{Emerald}
%% code by Andrew Stacey
% https://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582
\makeatletter
\tikzset{%
remember picture with id/.style={%
remember picture,
overlay,
draw=\bcol,
save picture id=#1,
},
save picture id/.code={%
\edef\pgf@temp{#1}%
\immediate\write\pgfutil@auxout{%
\noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
},
if picture id/.code args={#1#2#3}{%
\@ifundefined{save@pt@#1}{%
\pgfkeysalso{#3}%
}{
\pgfkeysalso{#2}%
}
}
}
\def\savepointas#1#2{%
\expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}
\def\tmk@labeldef#1,#2\@nil{%
\def\tmk@label{#1}%
\def\tmk@def{#2}%
}
\tikzdeclarecoordinatesystem{pic}{%
\pgfutil@in@,{#1}%
\ifpgfutil@in@%
\tmk@labeldef#1\@nil
\else
\tmk@labeldef#1,(0pt,0pt)\@nil
\fi
\@ifundefined{save@pt@\tmk@label}{%
\tikz@scan@one@point\pgfutil@firstofone\tmk@def
}{%
\pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
\pgfsys@getposition{\pgfpictureid}\save@this@pic%
\pgf@process{\pgfpointorigin\save@this@pic}%
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgf@process{\pgfpointorigin\save@orig@pic}%
\advance\pgf@x by -\pgf@xa
\advance\pgf@y by -\pgf@ya
}%
}
\makeatother
\newcommand{\tikzmarkin}[1]{%
\tikz[remember picture with id=#1]
\draw[line width=1pt,rectangle,rounded corners,fill=\fcol]
(pic cs:#1) ++(0.1,-0.15) rectangle (-0.1,0.32)
;}
\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2] #1;}
\begin{document}
\[
\begin{array}{rcc@{+}c@{+}c@{+}cl}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
&&\tikzmarkin{a}23&0&7&5,\tikzmarkend{a} \\
&&30&0&9&0
&\}
\end{array}
\]
\end{document}
图形结果:
可以通过以下方式实现相同的结果:hf-tikz
包。代码如下:
\documentclass{article}
\usepackage{amsmath}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage[customcolors]{hf-tikz}
\hfsetfillcolor{Emerald!20}
\hfsetbordercolor{Emerald}
\begin{document}
\[
\begin{array}{rcc@{+}c@{+}c@{+}cl}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
&&\tikzmarkin{a}23&0&7&5,\tikzmarkend{a} \\
&&30&0&9&0
&\}
\end{array}
\]
\end{document}
答案3
tabu
您可以使用提供赞扬的包\rowfont
。
\documentclass{article}
\usepackage{amsmath}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{tabu}
\begin{document}
\[
\begin{tabu}{rcc@{+}c@{+}c@{+}cl}
solution&=&\min\{
&0&34&0&6, \\
&&0&23&5&18, \\
&&5&15&10&16, \\
&&10&10&9&10, \\
&&19&5&10&16, \\
\rowfont{\color{Emerald}} &&23&0&7&5, \\
&&30&0&9&0
&\}
\end{tabu}
\]
\end{document}