3

How to include such a path in a table?

table containing a path

this is the code proposed by @Schrodinger's cat with and the problem is when I changed "jeu" to "game" the result is not good:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
 \matrix[matrix of nodes,nodes in empty cells,
 nodes={minimum size=2.5em,draw,outer sep=0pt,anchor=center},
 row sep=-\pgflinewidth,column sep=-\pgflinewidth,
 column 1/.style={nodes={draw=none},align=right,text width={width("game")}},
 row 1/.style={nodes={draw=none},align=center},
 ] (mat) {
   & 0 & 15 & 30 & 40 & game \\
 0 &   &    &    &    & |[fill=purple!30]| \\
 15 &   &    &    &    & |[fill=purple!30]| \\
 30 &   &    &    &    & |[fill=purple!30]| \\
 40 &   &    &    &|[fill=green!70!black]|    &  \\
 game & |[fill=orange]|   &|[fill=orange]|   & |[fill=orange]| &    &  \\
 };
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}

pathlatex

8

Welcome! With a TikZ matrix it is very easy.

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
 \matrix[matrix of nodes,nodes in empty cells,
 nodes={minimum size=2.5em,draw,outer sep=0pt,anchor=center},
 row sep=-\pgflinewidth,column sep=-\pgflinewidth,
 column 1/.style={nodes={draw=none},align=right,text width={width("jeu")}},
 row 1/.style={nodes={draw=none},align=center},
 ] (mat) {
   & 0 & 15 & 30 & 40 & jeu \\
 0 &   &    &    &    & |[fill=purple!30]| \\
 15 &   &    &    &    & |[fill=purple!30]| \\
 30 &   &    &    &    & |[fill=purple!30]| \\
 40 &   &    &    &|[fill=green!70!black]|    &  \\
 jeu & |[fill=orange]|   &|[fill=orange]|   & |[fill=orange]| &    &  \\
 };
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: A version that does not require you to set the text width of the first column yourself. You need to compile twice.

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\newbox\matrixcellbox
\tikzset{column align/.style 2 args={column #1/.append style={nodes={execute at begin
node={\setbox\matrixcellbox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][#2]{\copy\matrixcellbox}}}}}}
\usepackage{eqparbox}
\begin{document}
\begin{tikzpicture}
 \matrix[matrix of nodes,nodes in empty cells,
 nodes={minimum size=3.25em,draw,outer sep=0pt,anchor=center},
 row sep=-\pgflinewidth,column sep=-\pgflinewidth,
 column 1/.style={nodes={draw=none}},column align={1}{r},
 row 1/.style={nodes={draw=none},align=center},
 ] (mat) {
   & 0 & 15 & 30 & 40 & game \\
 0 &   &    &    &    & |[fill=purple!30]| \\
 15 &   &    &    &    & |[fill=purple!30]| \\
 30 &   &    &    &    & |[fill=purple!30]| \\
 40 &   &    &    &|[fill=green!70!black]|    &  \\
 game & |[fill=orange]|   &|[fill=orange]|   & |[fill=orange]| &    &  \\
 };
\draw[red,very thick] plot[mark=*] coordinates {
    (mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}

enter image description here

One could play this game further to completely autosize the cells, if needed. Rather than doing this, here is a version in which you can specify the wides entry with widest=game, for example.

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{matrix}
\tikzset{widest/.style={nodes={minimum size={width("#1")+4*2pt+4*\pgflinewidth}},
column 1/.append style={nodes={text width={width("#1")}}}}}
\begin{document}
\begin{tikzpicture}
 \matrix[matrix of nodes,nodes in empty cells,
 nodes={draw,outer sep=0pt,anchor=center},widest=game,
 row sep=-\pgflinewidth,column sep=-\pgflinewidth,
 column 1/.append style={nodes={draw=none},align=right},
 row 1/.append style={nodes={draw=none},align=center},
 ] (mat) {
   & 0 & 15 & 30 & 40 & game \\
 0 &   &    &    &    & |[fill=purple!30]| \\
 15 &   &    &    &    & |[fill=purple!30]| \\
 30 &   &    &    &    & |[fill=purple!30]| \\
 40 &   &    &    &|[fill=green!70!black]|    &  \\
 game & |[fill=orange]|   &|[fill=orange]|   & |[fill=orange]| &    &  \\
 };
\draw[red,very thick] plot[mark=*] coordinates {
(mat-2-2) (mat-2-3) (mat-3-3) (mat-3-4) (mat-4-4) (mat-4-5) (mat-4-6)};
\end{tikzpicture}
\end{document}
  • Thank you very much, that was really helpful. – Math 512 Nov 16 at 19:10
  • cat when I change "jeu" to "game" the last column is like isolated from the rest. – Math 512 Nov 16 at 20:46
  • @Math512 In that case you need column 1/.style={nodes={draw=none},align=right,text width={width("game")}},, i.e. use the width of "game". One can, of course, automatize this with e.g. eqparbox. – Schrödinger's cat Nov 16 at 20:48
  • I still have the same problem as shown in my last edit to the question – Math 512 Nov 16 at 21:01
  • @Math512 Please see the lower code of my answer. (It would also be nice if you could mention where the code comes from.) – Schrödinger's cat Nov 16 at 21:01
1

Here is another way! Do you see plain TikZ code?

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[shift={(-.5,.5)}]
\fill[purple!30] (4,0)  rectangle +(1,-3);
\fill[cyan]      (3,-3) rectangle +(1,-1);
\fill[orange]    (0,-4) rectangle +(3,-1);
\draw (0,0) grid (5,-5);
\end{scope}

\draw[red,mark=*,line width=1.2pt] plot coordinates 
{(0,0) (1,0) (1,-1) (2,-1) (2,-2) (3,-2) (4,-2)};

\foreach \i/\j in {0/0,1/15,2/30,3/40,4/jeu}
\path (\i,1) node{\j} (-1,-\i) node{\j}; 
\end{tikzpicture}
\end{document}
  • 1
    hmmmmm, it's really interesting, I am new to the world of Tikz and I am already astonished by it. thank you @BLack for this new way. – Math 512 Nov 17 at 18:40
  • @Math512 my advice is starting with plain TikZ, and try to write code with elegant simplicity – Black Mild Nov 18 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.