5

So I wrote the following code:


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[export]{adjustbox}

\begin{document}

\begin{table}[H]

    \centering
    \begin{tabular}{ccccccc}
    \multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \\
    \headerrow & & 0 & 1 & 2 & 3 & 4 \\
    \multirow{5}{*}{\rotatebox{90}{Samples}} 


     & 0 & \(9.87*e^{-01}\) &  \(8.67*e^{-17} \)&  \(4.13*e^{-24}\) &  \(1.16*e^{-17}\) &  \(4.58*e^{-17}\) \\ 

    & 1 & \(1.16*e^{-16}\) &  \(9.99*e^{-01}\) &  \(9.14*e^{-25}\) &  \(3.63*e^{-22}\) &  \(2.69*e^{-11}\) \\

    & 2 & \(4.35*e^{-33}\) &  \(7.85*e^{-34}\) &  \(9.93*e^{-01}\) &  \(3.74*e^{-04}\) &  \(4.55*e^{-34}\) \\

    & 3 & \(5.51*e^{-26}\)&   \(5.13*e^{-31}\)&   \(5.02*e^{-04}\)    & \(9.99*e^{-01}\) &    \(1.71*e^{-29}\) \\

    & 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)&   \(4.64*e^{-20}\)&   \(9.99*e^{-01}\)

    \end{tabular}

    \label{tbl:predan}
    \end{table}


\end{document}

This is the output: enter image description here

So I want to put some boxes around some variables in tables. It would be good when the color can be chosen too. something like the next Foto.

enter image description here

Thanks in advance :)

Milad
  • 83

3 Answers3

5

I propose this code, with basic packages:

    \documentclass{article}
    \usepackage{array, multirow, bigstrut, float}
    \usepackage[table]{xcolor}
    \usepackage{hhline}
    \usepackage[export]{adjustbox}
\begin{document}

\begin{table}[H]
\setlength{\arrayrulewidth}{0.8pt}
\centering
\begin{tabular}{*{7}{c}}
\multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \\
headerrow & & 0 & 1 & 2 & 3 & 4 \\
\multirow{5}{*}{\rotatebox{90}{Samples}}

 & 0 & \(9.87*e^{-01}\) & \(8.67*e^{-17} \)& \(4.13*e^{-24}\) & \(1.16*e^{-17}\) & \(4.58*e^{-17}\) \\
\hhline{~~- >{\arrayrulecolor{red}}->{\arrayrulecolor{black}}-}
& 1 & \multicolumn{1}{|c}{\(1.16*e^{-16}\) \bigstrut[t]}&\multicolumn{1}{!{\color{red}\vrule width 0.8pt}c!{\color{red}\vrule width 0.8pt}}{ \(9.99*e^{-01}\) }& \multicolumn{1}{c|}{\(9.14*e^{-25}\)} & \(3.63*e^{-22}\) & \(2.69*e^{-11}\) \\
\hhline{~~- >{\arrayrulecolor{red}}->{\arrayrulecolor{black}}-}
& 2 & \(4.35*e^{-33}\) & \(7.85*e^{-34}\) & \(9.93*e^{-01}\) & \(3.74*e^{-04}\) & \(4.55*e^{-34}\) \\

& 3 & \(5.51*e^{-26}\)& \(5.13*e^{-31}\)& \(5.02*e^{-04}\) & \(9.99*e^{-01}\) & \(1.71*e^{-29}\) \\

& 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)& \(4.64*e^{-20}\)& \(9.99*e^{-01}\)

\end{tabular}

\label{tbl:predan}
\end{table}

\end{document}

enter image description here

Bernard
  • 271,350
4

With the latest version of nicematrix (v 5.13 of 2021/03/23), you have a command \Block with keys draw and borders to draw borders of a cell.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions{cell-space-limits=2pt}

\begin{NiceTabular}{ccccccc} & \Block{1-5}{Feature Vector of Samples} \ & & 0 & 1 & 2 & 3 & 4 \ \Block{5-1}{\rotate Samples} & 0 & (9.87e^{-01}) & (8.67e^{-17} )& (4.13e^{-24}) & (1.16e^{-17}) & (4.58e^{-17}) \ & 1 & \Block[borders={top,bottom,left}]{}{(1.16e^{-16})} & \Block[draw=red]{}{(9.99e^{-01})} &
\Block[borders={top,bottom,right}]{}{(9.14
e^{-25})} & (3.63e^{-22}) & (2.69e^{-11}) \ & 2 & (4.35e^{-33}) & (7.85e^{-34}) & (9.93e^{-01}) & (3.74e^{-04}) & (4.55e^{-34}) \ & 3 & (5.51e^{-26}) & (5.13e^{-31})& (5.02e^{-04})& (9.99e^{-01}) & (1.71e^{-29}) \ & 4 & (9.60e^{-18}) & (2.21e^{-07}) & (2.52e^{-25}) & (4.64e^{-20}) & (9.99*e^{-01}) \end{NiceTabular}

\end{document}

If you prefer to have the instructions of formatting after the array, it's possible. In the environments of nicematrix you can, in the \CodeAfter, use Tikz to draw whatever rule you want.

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

\begin{document}

\NiceMatrixOptions{cell-space-limits=2pt}

\begin{NiceTabular}{ccccccc} & \Block{1-5}{Feature Vector of Samples} \ & & 0 & 1 & 2 & 3 & 4 \ \Block{5-1}{\rotate Samples} & 0 & (9.87e^{-01}) & (8.67e^{-17} ) & (4.13e^{-24}) & (1.16e^{-17}) & (4.58e^{-17}) \ & 1 & (1.16e^{-16}) & (9.99e^{-01}) & (9.14e^{-25}) & (3.63e^{-22}) & (2.69e^{-11}) \ & 2 & (4.35e^{-33}) & (7.85e^{-34}) & (9.93e^{-01}) & (3.74e^{-04}) & (4.55e^{-34}) \ & 3 & (5.51e^{-26}) & (5.13e^{-31})& (5.02e^{-04})& (9.99e^{-01}) & (1.71e^{-29}) \ & 4 & (9.60e^{-18}) & (2.21e^{-07}) & (2.52e^{-25}) & (4.64e^{-20}) & (9.99*e^{-01}) \CodeAfter \begin{tikzpicture} \draw (4-|4) -- (4-|3) -- (5-|3) -- (5-|4) ; \draw (4-|5) -- (4-|6) -- (5-|6) -- (5-|5) ; \draw [red] (4-|4) rectangle (5-|5) ; \end{tikzpicture} \end{NiceTabular}

\end{document}

With both syntax, the ouput is as follows.

Output of the above code

You need several commpilations (because nicematrix uses PGF/Tikz nodes).

F. Pantigny
  • 40,250
3

For anyone interested in the answer, I used tikzmark to draw the boxes.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage[export]{adjustbox}
\usepackage{tikz}
\usetikzlibrary{fit,tikzmark}   
\usepackage{booktabs,cellspace}

\begin{document}

\begin{table} \centering \begin{tabular}{ccccccc} \multicolumn{2}{c}{} &\multicolumn{5}{c}{Feature Vector of Samples} \ \headerrow & & 0 & 1 & 2 & 3 & 4 \ \multirow{5}{*}{\rotatebox{90}{Samples}}

     & 0 & \(9.87*e^{-01}\) &  \(8.67*e^{-17} \)&  \(4.13*e^{-24}\) &  \(1.16*e^{-17}\) &  \(4.58*e^{-17}\) \\ 

    & 1 & \tikzmarknode{a}{\(1.16*e^{-16}\)} & \tikzmarknode{c}{\(9.99*e^{-01}\)} &  \tikzmarknode{b}{\(9.14*e^{-25}\)} &  \(3.63*e^{-22}\) & \(2.69*e^{-11}\)  \\

    & 2 & \(4.35*e^{-33}\) &  \(7.85*e^{-34}\) &  \(9.93*e^{-01}\) &  \(3.74*e^{-04}\) &  \(4.55*e^{-34}\) \\

    & 3 & \(5.51*e^{-26}\)&   \(5.13*e^{-31}\)&   \(5.02*e^{-04}\)    & \(9.99*e^{-01}\) &    \(1.71*e^{-29}\) \\

    & 4 & \(9.60*e^{-18}\) & \(2.21*e^{-07}\)& \(2.52*e^{-25}\)&   \(4.64*e^{-20}\)&   \(9.99*e^{-01}\)

\end{tabular}
\label{tbl:predan}

\end{table}

\begin{tikzpicture}[overlay,remember picture] \node[draw=red, thin, inner sep=1.5pt, fit=(a) (b)] {}; \node[draw=green, thin, inner sep=1.5pt,rounded corners, fit=(c) (c)] {}; \end{tikzpicture}

\end{document}

enter image description here

Milad
  • 83