0

I want to generate the table below:

desired output

I wanted to replicate in latex:

  1. no border except the horizontal and vertical lines around the labels A-E and O-D

  2. spaced out rows and columns so the entries don't look too jammed

  3. a red arrow that traces some of the numbers on the table

I have gotten as far as below:

what I got

and this is obtained with the attached code below:

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyheadings}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{stackengine,graphicx,xcolor}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepackage{lscape}
\usetikzlibrary{arrows,automata}
\usepackage[thinlines]{easytable}
\usepackage[latin1]{inputenc}
\usetikzlibrary{arrows}
\usetikzlibrary{trees}
\usepgfplotslibrary{fillbetween}
\begin{document}
\setlength{\parindent}{0cm}
%\pagecolor{blue!13}
\fancyhf{}
\fancyhead[C]{\thepage}
\voffset = 0.1cm
\pagestyle{fancy}
\definecolor{Elite}{RGB}{37,97,172}
\definecolor{web}{RGB}{56, 102, 166}
\setlength{\parindent}{0mm}
\setcounter{page}{1}
$\begin{array}{c|ccccc}
\midrule
O & A & B & C & D & E\\
\midrule
O & \fbox{1} & 5 & X & X & X\\
A & \fbox{1} & 5 & \fbox{4} & 10 & X\\
C & \fbox{1} & \fbox{5} & \fbox{4} & 8 & X\\
B & \fbox{1} & \fbox{5} & \fbox{4} & \fbox{8} & 11\\
D & \fbox{1} & \fbox{5} & \fbox{4} & \fbox{8} & \fbox{10}\\
\end{array}$
%
\end{document}

Latex experts, can you please suggest the modifications so I achieve what I want in this case?

Thanks for your help in advance.

2

Something like this:

enter image description here

I have used a tikz matrix of nodes with a few bells and whistles.

The boxed entries are done using

boxed/.style args={#1/#2}{row #1 column #2/.style={nodes={rectangle, draw}}}

combined with a .list handler for setting the coordinates to be boxed, which is just a comma separated list of row/column pairs.

The red arrows I have thrown into a \foreach loop.

Of course, you can add as much styling as you like. I have shown how to write the entries in the first row in blue.

Here is the full code:

\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.markings,decorations.text,calc}
\begin{document}

  \tikzset{
     boxed/.style args={#1/#2}{row #1 column #2/.style={nodes={rectangle, draw}}},
     ->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{stealth}}},
              postaction={decorate}}
  }

  \begin{tikzpicture}[every node/.style={minimum width=1.6em},
      boxed/.list={2/2,3/2,4/2,5/2,6/2,4/3,5/3,6/3,3/4,4/4,5/4,6/4,5/5,6/5,6/6},
    ]
    \matrix (M)[matrix of math nodes, column sep=1mm, row sep=1mm,
                row 1/.style={nodes={text=blue}}
    ]{
          O & A & B & C & D & E\\
          O & 1 & 5 & X & X & X\\
          A & 1 & 5 & 4 & 10 & X\\
          C & 1 & 5 & 4 & 8 & X\\
          B & 1 & 5 & 4 & 8 & 11\\
          D & 1 & 5 & 4 & 8 & 10\\
     };
     \draw[thin](M-1-1.north west)--(M-1-6.north east);
     \draw[thin]($ (M-2-1.north west)+(0,0.1) $)--($ (M-2-6.north east)+(0,0.1) $);
     \draw[thin](M-1-1.north east)--(M-6-1.south east);
     \foreach \x/\y [remember=\x as \xx (initially 6),
                     remember=\y as \yy (initially 6)]
                 in {6/5,4/5,4/4,3/4,3/2,2/2} {
       \draw[red,->-](M-\xx-\yy.center)--(M-\x-\y.center);
     }
  \end{tikzpicture}

\end{document}
2

So I'm definitely no expert -- but I'd do this as a diagram using \tikz not as a picture. I'm sure that someone who knows what they are doing can get the nodes to be placed automatically rather than by hand! And obviously you can play with how lines are drawn, whether to fill nodes, and so forth.

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzstyle{boxed} = [draw,rectangle,thick,minimum height=2em,minimum width=2em]
\tikzstyle{unboxed} = [minimum height=2em,minimum width=2em]
\tikzstyle{arrowline} = [thick,color=red,->]
\begin{document}
\begin{tikzpicture}\bfseries
  \node at (0,0) {D};
  \node at (0,1) {B};
  \node at (0,2) {C};
  \node at (0,3) {A};
  \node at (0,4) {O};
  \node[boxed] at (1,0) {1};
  \node[boxed] at (2,0) {5};
  \node[boxed] at (3,0) {4};
  \node[boxed,fill=red!50](B) at (4,0) {8};
  \node[boxed,fill=red!50](A) at (5,0) {10};
  \node[boxed] at (1,1) {1};
  \node[boxed] at (2,1) {5};
  \node[boxed] at (3,1) {4};
  \node[boxed,fill=red!50](C) at (4,1) {8};
  \node at (5,1) {11};
  \node[boxed] at (1,2) {1};
  \node[boxed] at (2,2) {5};
  \node[boxed,fill=red!50](D) at (3,2) {4};
  \node[unboxed,fill=red!50] at (4,2)(H) {8};
  \node at (5,2) {$\times$};
  \node[boxed,fill=red!50](F) at (1,3) {1};
  \node[unboxed,fill=red!50](I) at (2,3) {5};
  \node[boxed,fill=red!50](E) at (3,3) {4};
  \node at (4,3) {10};
  \node at (5,3) {$\times$};
  \node[boxed,fill=red!50](G) at (1,4) {1};
  \node at (2,4) {5};
  \node at (3,4) {$\times$};
  \node at (4,4) {$\times$};
  \node at (5,4) {$\times$};
  \node at (1,5) {A};
  \node at (2,5) {B};
  \node at (3,5) {C};
  \node at (4,5) {D};
  \node at (5,5) {E};
  \draw[color=blue](-0.5,4.6) -- (5.5,4.6);
  \draw[color=blue](0.4,5.5) -- (0.4,-0.5);
  \draw[arrowline] (A.west) -- (B.east);
  \draw[arrowline] (B.north) -- (C.south);
  \draw[arrowline] (C.north) -- (H.south);
  \draw[arrowline] (H.west) -- (D.east);
  \draw[arrowline] (D.north) -- (E.south);
  \draw[arrowline] (E.west) -- (I.east);
  \draw[arrowline] (I.west) -- (F.east);
  \draw[arrowline] (F.north) -- (G.south);

\end{tikzpicture}
\end{document}

Table

  • 1
    Thanks. But Andrew's is better code and easily adaptable to produce the same result! – Paul Stanley Apr 10 '18 at 12:17

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.