5

What is the most elegant way to reach that style (by using a Tikz-matrix):

enter image description here

MWE:
€dit: I set a simplification at the lists. This makes it clearer which elements should be emphasized.

\documentclass[margin=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \begin{tikzpicture}[] \def\A{A} \def\B{B} \def\C{C} \def\D{D} \matrix[matrix of nodes, column sep=0.125em, row sep=0.1525em, fill=gray!33, anchor=north, rs/.style={row #1/.style={row sep=0.525em}}, rs/.list={6,12,18}, emp/.style 2 args={row #1 column #2/.style={nodes={fill=pink}}}, emp/.list={{1}{1},{2}{1},{3}{1},{4}{1},{5}{1},{6}{1}, {7}{2},{8}{2},{9}{2},{10}{2},{11}{2},{12}{2}, {13}{3},{14}{3},{15}{3},{16}{3},{17}{3},{18}{3}, {19}{4},{20}{4},{21}{4},{22}{4},{23}{4},{24}{4} }, ] (s1) at (0,0){ \A & \B & \C & \D \\ \A & \B & \D & \C \\ \A & \C & \B & \D \\ \A & \C & \D & \B \\ \A & \D & \B & \C \\ \A & \D & \C & \B \\ % \A & \B & \C & \D \\ \A & \B & \D & \C \\ \C & \B & \A & \D \\ \C & \B & \D & \A \\ \D & \B & \A & \C \\ \D & \B & \C & \A \\ % \A & \B & \C & \D \\ \A & \D & \C & \B \\ \B & \A & \C & \D \\ \B & \D & \C & \A \\ \D & \A & \C & \B \\ \D & \B & \C & \A \\ % \A & \B & \C & \D \\ \A & \C & \B & \D \\ \B & \A & \C & \D \\ \B & \C & \A & \D \\ \C & \A & \B & \D \\ \C & \B & \A & \D \\ }; \end{tikzpicture} \end{document} 
4
  • 2
    Does it need to be a tikz matrix? This would be very easy with tabularray. Commented Oct 6 at 8:59
  • @samcarter_is_at_topanswers.xyz Yes, because this is a submatrix of a bigger matrix a.s.o. Commented Oct 6 at 9:00
  • 1
    @cis could you add some other elements of "a bigger matrix" as the example to show the necessity of using tikz-matrix here? Commented Oct 6 at 9:14
  • There are always other options. However, I'd like to stick with the TikZ-matrix here. Note: As an edit, I've combined the lists of highlighted elements into one list (with two arguments). Commented Oct 6 at 11:11

1 Answer 1

4

I’m not sure how elegant this approach is—after all, elegance is quite subjective. We simply generate the highlighting styles programmatically using nested \foreach loops.

\documentclass[margin=24pt]{standalone} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \begin{tikzpicture} \def\A{A} \def\B{B} \def\C{C} \def\D{D} % --- Generate a list of styles for pink-highlighted cells --- \def\highlightlist{} \foreach \block in {0,1,2,3} { \foreach \i in {1,...,6} { \pgfmathtruncatemacro{\row}{\block*6 + \i} \pgfmathtruncatemacro{\col}{\block + 1} \ifx\highlightlist\empty \xdef\highlightlist{row \row\space column \col/.style={nodes={fill=pink}}} \else \xdef\highlightlist{\highlightlist, row \row\space column \col/.style={nodes={fill=pink}}} \fi } } % --- Matrix with \\[...] to add extra vertical space between blocks --- \expanded{% \noexpand\matrix[ matrix of nodes, column sep=0.125em, row sep=0.1525em, fill=gray!33, \highlightlist ] } (s1) { \A & \B & \C & \D \\ \A & \B & \D & \C \\ \A & \C & \B & \D \\ \A & \C & \D & \B \\ \A & \D & \B & \C \\ \A & \D & \C & \B \\[0.525em] \A & \B & \C & \D \\ \A & \B & \D & \C \\ \C & \B & \A & \D \\ \C & \B & \D & \A \\ \D & \B & \A & \C \\ \D & \B & \C & \A \\[0.525em] \A & \B & \C & \D \\ \A & \D & \C & \B \\ \B & \A & \C & \D \\ \B & \D & \C & \A \\ \D & \A & \C & \B \\ \D & \B & \C & \A \\[0.525em] \A & \B & \C & \D \\ \A & \C & \B & \D \\ \B & \A & \C & \D \\ \B & \C & \A & \D \\ \C & \A & \B & \D \\ \C & \B & \A & \D \\ }; \end{tikzpicture} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.