4

I am new to latex and I need to create something as shown in the picture. enter image description here

So far this is what i can create. Please keep the code simple

\begin{center}
\begin{tabular}{|c|c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\ \hline
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\end{tabular}
\end{center}

2 Answers 2

5

Welcome to TeX.SE! Something like this?

\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{center}
\tikzmarknode{tab}{\begin{tabular}{|c!{\color{red}\vline}c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\ 
\arrayrulecolor{red}\hline\arrayrulecolor{black}
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\end{tabular}}
\begin{tikzpicture}[overlay,remember picture]
\draw[thick,green!60!black,shorten <=6mm] (tab.north west) -- (tab.south east);
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Or with selected lines thicker but slightly more complicated code.

\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\newlength{\savedwidth}
\begin{document}
\begin{center}
\tikzmarknode{tab}{\global\savedwidth\arrayrulewidth % p. 6 of colortbl docu
\arrayrulewidth0.2pt
\begin{tabular}{|c!{\arrayrulewidth0.8pt\color{red}\vline}c|c|c|c|}\hline
$ \star $ & 1 & 2 & 3 & 4\\ 
\noalign{\global\setlength\arrayrulewidth{0.8pt}}%
\arrayrulecolor{red}\hline\arrayrulecolor{black}\noalign{\global\arrayrulewidth0.2pt}
1 & 4 & 3 & 2 & 1\\ \hline
2 & 3 & 1 & 4 & 2\\ \hline
3 & 2 & 4 & 1 & 3\\ \hline
4 & 1 & 2 & 3 & 4\\ \hline
\end{tabular}}\global\arrayrulewidth\savedwidth
\begin{tikzpicture}[overlay,remember picture]
\draw[line width=0.8pt,green!60!black,shorten <=6mm,shorten >=1pt] (tab.north west) -- (tab.south east);
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

3
  • @SamBluez You're welcome!
    – user121799
    Jan 20, 2019 at 8:33
  • just a question. Can i tweak this to draw a: vertical line across column 5 and a horizontal line across row 5 instead of the diagonal line, so i can use it in my next example. also the horizontal and vertical lines should start from the second element ie not include the top row and left column
    – Sam Bluez
    Jan 20, 2019 at 8:49
  • @SamBluez Would you mind asking a separate, i.e. new question for that? ( I am going to bed and there are so many experts around who will help you.)
    – user121799
    Jan 20, 2019 at 9:10
1

Here is a solution with {NiceTabular} of nicematrix (≥ 6.23 2023-09-02). That environment is similar to the classical environment {tabular} (as provided by the package array) but creates PGF/TikZ nodes under the cells, nodes and columns. Then it's possible to use TikZ in the so-called \CodeAfter to draw whatever rule you want.

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

\begin{document}

\begin{center}

\begin{NiceTabular}{|c|[color=red]c|c|c|c|}\hline
  $ \star $ & 1 & 2 & 3 & 4\\ \Hline[color=red]
  1 & 4 & 3 & 2 & 1\\ \hline
  2 & 3 & 1 & 4 & 2\\ \hline
  3 & 2 & 4 & 1 & 3\\ \hline
  4 & 1 & 2 & 3 & 4\\ \hline
\CodeAfter
  \tikz \draw [thick, green, shorten < = -1mm ] (2-|2) -- (last-|last) ; 
\end{NiceTabular}

\end{center}

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .