[Tex/LaTex] Produce a tree diagram to display football matchup

diagramstikz-trees

I am working on a Math Project involving football matches. I hope I can produce a tree diagram like this. Could anyone help me with the basic code? I feel producing this diagram requires tikz, which I am not good at. Thanks for your help!
enter image description here

By the way, I do not need the text regarding the date and the place of the matches. Thanks again for your attention!

Best Answer

This is a forest solution which involves a little less typing than some other possibilities.

The basic idea is to see the diagram as a tree with an invisible root note on the far right. This has two children: the final and the detached third place match. To avoid affecting the main tree, the detached node is drawn after all of the other nodes are typeset and their positions set.

matchnode={<flag-1>}{<team-1>}{<score-1>}{<flag-2>}{<team-2>}{<score-3>} is a style used to define the tabular content for each match. Each <flag> should be the name of the image file containing the relevant <team>'s flag.

round name={<name>} is a style used to define the competition stages marked at the top. The title of the third place match is added directly to the relevant node, since this is a one off.

The colours are set as lineblue, fillblue and textblue. The overall appearance of all nodes is set using the footnode style which can be modified as desired. The general shape and thickness of the branches is set using forest's edge. If you want straight edges, for example, just say edge={ultra thick}, removing the rounded corners... specification.

\PassOptionsToPackage{dvipsnames,svgnames,x11names,rgb,table}{xcolor}
\documentclass[tikz,border=10pt]{standalone}
\usepackage{array,forest}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usetikzlibrary{positioning,shadows}
\colorlet{textblue}{blue!75!black}
\colorlet{lineblue}{blue}
\colorlet{fillblue}{blue!10}
\newlength\Tht
\settoheight{\Tht}{T}
\begin{document}
\arrayrulecolor{lineblue}
\begin{forest}
  /tikz/footnode/.style={
    font=\sffamily,
    text=textblue,
    draw=lineblue,
    inner color=fillblue!75,
    outer color=fillblue,
    drop shadow,
    rounded corners=1pt,
  },
  matchnode/.style n args=6{
    footnode,
    inner sep=0pt,
    content={\includegraphics[height=\Tht]{#1} \textcolor{textblue}{#2}&\textcolor{black}{#3}\\\hline\includegraphics[height=\Tht]{#4} \textcolor{textblue}{#5}&\textcolor{black}{#6}},
  },
  round name/.style={
    tikz={\node [above=of .center |- pen.north, anchor=mid, footnode, minimum height=4ex] {#1};}
  },
  match align/.style={
    align={l|c},
    parent anchor=west,
    child anchor=east,
    anchor=west,
  },
  for tree={
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(-10pt,0) |- (.child anchor)\forestoption{edge label};
    },
    l sep+=20pt,
    match align,
    grow=180,
    edge={ultra thick, rounded corners},
    footnode,
    tier/.wrap pgfmath arg={tier #1}{level()},
  },
  [, phantom,
    before drawing tree={
      append={[, match align, matchnode={example-image-a}{Team E}{0}{example-image-b}{Team M}{2}, typeset node, afterthought={\node [above=10pt of .north, anchor=south, footnode, minimum height=4ex] {Third place};}]},
    }
    [, matchnode={example-image-a}{Team A}{1}{example-image-b}{Team I}{0}, round name=Final
      [, matchnode={example-image-a}{Team A}{1}{example-image-b}{Team E}{0}, round name=Semi-Finals
        [, matchnode={example-image-a}{Team A}{1}{example-image-b}{Team C}{0}, round name=Quarter-Finals
          [, matchnode={example-image-a}{Team A}{1}{example-image-b}{Team B}{0}, name=pen, round name=Round of 16]
          [, matchnode={example-image-a}{Team C}{1}{example-image-b}{Team D}{0}]
        ]
        [, matchnode={example-image-a}{Team E}{2}{example-image-b}{Team G}{1}
          [, matchnode={example-image-a}{Team E}{1}{example-image-b}{Team F}{0}]
          [, matchnode={example-image-a}{Team G}{1}{example-image-b}{Team H}{0}]
        ]
      ]
      [, matchnode={example-image-a}{Team I}{1}{example-image-b}{Team M}{0}
        [, matchnode={example-image-a}{Team I}{1}{example-image-b}{Team K}{0}
          [, matchnode={example-image-a}{Team I}{0 (4)}{example-image-b}{Team J}{0 (2)}]
          [, matchnode={example-image-a}{Team K}{1}{example-image-b}{Team L}{0}]
        ]
        [, matchnode={example-image-a}{Team M}{1}{example-image-b}{Team O}{0}
          [, matchnode={example-image-a}{Team M}{1}{example-image-b}{Team N}{0}]
          [, matchnode={example-image-a}{Team O}{1}{example-image-b}{Team P}{0}]
        ]
      ]
    ]
  ]
\end{forest}

\end{document}

football world cup

EDIT

Here's another version which uses a freeware font to supply the flag symbols, and a colour series to randomly apply colours to them. Instead of specifying the image file of the relevant flag, the first and fourth arguments of the matchnode style now specify the relevant character from the freeware font.

This solution requires lualatex. Note that xelatex will NOT work. (And, of course, pdflatex, latex etc. won't either as the code requires fontspec.)

\PassOptionsToPackage{dvipsnames,svgnames,x11names,rgb,table}{xcolor}
\documentclass[tikz,border=10pt]{standalone}
\usepackage{array,forest}
\usepackage{fontspec}
\newfontfamily\fflag{Flags.ttf}
\usetikzlibrary{positioning,shadows}
\colorlet{textblue}{blue!75!black}
\colorlet{lineblue}{blue}
\colorlet{fillblue}{blue!10}
\newlength\Tht
\settoheight{\Tht}{T}
% xcolor manual: 34
\definecolorseries{colours}{hsb}{grad}[hsb]{.575,1,1}{.987,-.234,0}
\resetcolorseries[12]{colours}
\begin{document}
\arrayrulecolor{lineblue}
\begin{forest}
  /tikz/footnode/.style={
    font=\sffamily,
    text=textblue,
    draw=lineblue,
    inner color=fillblue!75,
    outer color=fillblue,
    drop shadow,
    rounded corners=1pt,
  },
  matchnode/.style n args=6{
    footnode,
    inner sep=0pt,
    content={\raisebox{-.25em}{\fflag\color{colours!!+}#1} \textcolor{textblue}{#2}&\textcolor{black}{#3}\\\hline\raisebox{-.25em}{\fflag\color{colours!!+}#4} \textcolor{textblue}{#5}&\textcolor{black}{#6}},
  },
  round name/.style={
    tikz={\node [above=of .center |- pen.north, anchor=mid, footnode, minimum height=4ex] {#1};}
  },
  match align/.style={
    align={l|c},
    parent anchor=west,
    child anchor=east,
    anchor=west,
  },
  for tree={
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(-10pt,0) |- (.child anchor)\forestoption{edge label};
    },
    l sep+=20pt,
    match align,
    grow=180,
    edge={ultra thick, rounded corners},
    footnode,
    tier/.wrap pgfmath arg={tier #1}{level()},
  },
  [, phantom,
    before drawing tree={
      append={[, match align, matchnode={E}{Team E}{0}{M}{Team M}{2}, typeset node, afterthought={\node [above=10pt of .north, anchor=south, footnode, minimum height=4ex] {Third place};}]},
    }
    [, matchnode={A}{Team A}{1}{I}{Team I}{0}, round name=Final
      [, matchnode={A}{Team A}{1}{E}{Team E}{0}, round name=Semi-Finals
        [, matchnode={A}{Team A}{1}{C}{Team C}{0}, round name=Quarter-Finals
          [, matchnode={A}{Team A}{1}{B}{Team B}{0}, name=pen, round name=Round of 16]
          [, matchnode={C}{Team C}{1}{D}{Team D}{0}]
        ]
        [, matchnode={E}{Team E}{2}{G}{Team G}{1}
          [, matchnode={E}{Team E}{1}{F}{Team F}{0}]
          [, matchnode={G}{Team G}{1}{H}{Team H}{0}]
        ]
      ]
      [, matchnode={I}{Team I}{1}{M}{Team M}{0}
        [, matchnode={I}{Team I}{1}{K}{Team K}{0}
          [, matchnode={I}{Team I}{0 (4)}{J}{Team J}{0 (2)}]
          [, matchnode={K}{Team K}{1}{L}{Team L}{0}]
        ]
        [, matchnode={M}{Team M}{1}{O}{Team O}{0}
          [, matchnode={M}{Team M}{1}{N}{Team N}{0}]
          [, matchnode={O}{Team O}{1}{P}{Team P}{0}]
        ]
      ]
    ]
  ]
\end{forest}

\end{document}

Font flag version with LuaLaTeX

The font I used is 'Flags' by Sunwalk and is available here.