# Color a line in array

Is it possible to change a color of a single line inside an array environment?

I have an array of possible solutions and I'd like the best one to be highlighted, the array is declared like

\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}


I can get some result using

\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}


But actually this requires too many TeX commands — couldn't it be done simplier? And this solution does not highlight pluses between columns. I want these pluses to be separators so that numeric substitutions be more clear throughout my solution.

-
It is described in UK-FAQ: How to change a whole row of a table, which uses array package and simplified syntax (similar to @egreg's solution). Anyway, I'd prefer @MacroDaniel's solution using more modern tabu package. –  Leo Liu May 12 '12 at 12:52

The array package allows for inserting tokens at the start of cells:

\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}


-
Thank you, that's exactly what I need. :) –  gluk47 May 12 '12 at 12:51

Another solution could be highlight the background of the row. Here I provide a solution based on the \tikzmark macro (basic idea taken from Background coloring with overlay specification in algorithm2e + beamer package).

The code:

\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
% http://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}%
}%
}
\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}


The graphical result:

The same result can be achieved by means of the hf-tikz package. The code:

\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}

-

You can use the package tabu which provides the commend \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}

-
That simplifies coloring much, thank you. Anyway, is it some method to color pluses tied to column separatrs? –  gluk47 May 12 '12 at 12:43