Graphics, Figures & Tablespgfplotstable: font color in certain rows

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Phu87
Posts: 3
Joined: Fri Dec 27, 2024 10:48 pm

pgfplotstable: font color in certain rows

Post by Phu87 »

Hello together,

I want to color certain rows in a pgfplotstable lets say in red. I found a code snippet that colors the content of one cell. Below you can find an MWE in LaTeX beamer. The first table is done with pgfplots but only colors one cell. The second table is the target result, which is coded "traditionally". How can I achieve the target result in pgfplotstable?

Code: Select all

\documentclass[10pt,aspectratio=169]{beamer}

\usepackage[sfdefault]{noto}
\usepackage[T1]{fontenc}
\usefonttheme[onlymath]{serif} % math in serif but noto
\usefonttheme{professionalfonts} % math in default serif math font

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{colortbl}

\usepackage[main=ngerman, english]{babel} % German date formats

\begin{document}
	\begin{frame}[plain, fragile]
		\setlength{\extrarowheight}{2pt}
		\begin{center}
			\begin{tikzpicture}
				\node[inner sep=0pt, scale=1.0] {
					\pgfplotstabletypeset[
					col sep = &, row sep =  \\, 
					columns/v/.style={column type=l|, string type, column name={},
						postproc cell content/.append style={
							/pgfplots/table/@cell content/.add={$\pmb}{$},
						},
					},
					columns/x1/.style={column name={X},fixed, fixed zerofill=true,precision=2, dec sep align},
					columns/x2/.style={column name={Y},fixed, fixed zerofill=true,precision=2, dec sep align},
					every head row/.style={after row=\hline},  
					my special cell/.style={@cell content=\color{red}#1},
					every row 1 column 0/.append style={
						postproc cell content/.append code={%
							\pgfkeysgetvalue{/pgfplots/table/@cell content}{\myTmpVal}%
							\pgfkeysalso{my special cell/.expand once={\myTmpVal}}%
						},
					},
					]{
						v & x1 & x2 \\
						{P}_1 & 43.46 & 28.77 \\
						{P}_{1,2} & 70.68 & 29.79 \\
						{P}_{2,1} & 85.23 & 2.13 \\
						{P}_2 & 84.36 & 26.06 \\
						{P}_{2,2} & 83.85 & 40.01 \\
						{P}_{3,1} & 62.15 & 49.34 \\
						{P}_3 & 32.94 & 52.91 \\
						{P}_{3,2} & 11.95 & 32.81 \\
						{P}_{4,1} & 0.02 & 4.73 \\
						{P}_4 & 18.63 & 2.07 \\
						{P}_{4,2} & 32.03 & 0.15 \\
						{P}_{1,1} & 21.27 & 27.93 \\
						{P}_1 & 43.46 & 28.77 \\
					} 
				};
			\end{tikzpicture}
		\end{center}
	\end{frame}
	\begin{frame}[plain]		
		\begin{table}[h]
			\centering
			\begin{tabular}{l|cc}
				& X & Y\\
				\hline
				$\pmb{P}_1$ & $43.46$ & $28.77$ \\
				\textcolor{red}{$\pmb{P}_{1,2}$} & \textcolor{red}{$70.68$} & \textcolor{red}{$29.79$} \\
				\textcolor{red}{$\pmb{P}_{2,1}$} & \textcolor{red}{$85.23$} & \textcolor{red}{$2.13$} \\
				$\pmb{P}_2$ & $84.36$ & $26.06$ \\
				\textcolor{red}{$\pmb{P}_{2,2}$} & \textcolor{red}{$83.85$} & \textcolor{red}{$40.01$} \\
				\textcolor{red}{$\pmb{P}_{3,1}$} & \textcolor{red}{$62.15$} & \textcolor{red}{$49.34$} \\
				$\pmb{P}_3$ & $32.94$ & $52.91$ \\
				\textcolor{red}{$\pmb{P}_{3,2}$} & \textcolor{red}{$11.95$} & \textcolor{red}{$32.81$} \\
				\textcolor{red}{$\pmb{P}_{4,1}$} & \textcolor{red}{$0.02$} & \textcolor{red}{$4.73$} \\
				$\pmb{P}_4$ & $18.63$ & $2.07$ \\
				\textcolor{red}{$\pmb{P}_{4,2}$} & \textcolor{red}{$32.03$} & \textcolor{red}{$0.15$} \\
				\textcolor{red}{$\pmb{P}_{1,1}$} & \textcolor{red}{$21.27$} & \textcolor{red}{$27.93$} \\
				$\pmb{P}_1$ & $43.46$ & $28.77$ \\  
			\end{tabular}
		\end{table}
	\end{frame}
	
\end{document}
Thank you in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Bartman
Posts: 367
Joined: Fri Jan 03, 2020 2:39 pm

Re: pgfplotstable: font color in certain rows

Post by Bartman »

Please share your crossposts.
Phu87
Posts: 3
Joined: Fri Dec 27, 2024 10:48 pm

Re: pgfplotstable: font color in certain rows

Post by Phu87 »

Hello @Bartman,

You are right, I also asked the same problem on tex.stackexchange.com: pgfplotstable: font color of a row. I somehow want to find a solution with pgfplotstable. Pgfplotstable has so many cool features. I wonder that it is so difficult to color specific rows.
Phu87
Posts: 3
Joined: Fri Dec 27, 2024 10:48 pm

Re: pgfplotstable: font color in certain rows

Post by Phu87 »

Hello together,

I finally found a solution based on two other discussions:
pgfplotstable: how can I change the color of a cell's content while maintaining the style?
pgfplotstable: Highlight rows of a numeric sequence

Code: Select all

% https://tex.stackexchange.com/questions/563478/pgfplotstable-highlight-rows-of-a-numeric-sequence


\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\pgfplotstableset{my special cell/.style={@cell content=\color{red}#1}}
\pgfplotstableset{color row/.style=
	{
		postproc cell content/.append code={
			\count0=\pgfplotstablerow
			\advance\count0 by0
			\ifnum\count0=#1
			\pgfkeysgetvalue{/pgfplots/table/@cell content}{\myTmpVal}%
			\pgfkeysalso{my special cell/.expand once={\myTmpVal}}%
			\fi},     
		},      
		}

\begin{document}

	\pgfplotstabletypeset[
	col sep = &, row sep =  \\, 
	columns/v/.style={column type=l|, string type, column name={}},
	columns/x1/.style={fixed, fixed zerofill=true,precision=2, dec sep align},
	columns/x2/.style={fixed, fixed zerofill=true,precision=2, dec sep align},
	every head row/.style={after row=\hline}, 
	color row/.list={1,2,4,5,7,8,10,11},
	]{
		v & x1 & x2 \\
		$\pmb{P}_1$ & 43.46 & 28.77 \\
		$\pmb{P}_{1,2}$ & 70.68 & 29.79 \\
		$\pmb{P}_{2,1}$ & 85.23 & 2.13 \\
		$\pmb{P}_2$ & 84.36 & 26.06 \\
		$\pmb{P}_{2,2}$ & 83.85 & 40.01 \\
		$\pmb{P}_{3,1}$ & 62.15 & 49.34 \\
		$\pmb{P}_3$ & 32.94 & 52.91 \\
		$\pmb{P}_{3,2}$ & 11.95 & 32.81 \\
		$\pmb{P}_{4,1}$ & 0.02 & 4.73 \\
		$\pmb{P}_4$ & 18.63 & 2.07 \\
		$\pmb{P}_{4,2}$ & 32.03 & 0.15 \\
		$\pmb{P}_{1,1}$ & 21.27 & 27.93 \\
		$\pmb{P}_1$ & 43.46 & 28.77 \\
	}
	
\end{document}
pgfplots-table.png
pgfplots-table.png (26.49 KiB) Viewed 1416 times
I also posted the solution here:
pgfplotstable: font color of a row
Post Reply