# Building a TikZ-matrix from specific data

If I have a table like

1 | 3 | s   1 | 1 | r
5 | 3 | b   11 | 3 | b
3 | 1 | r   19 | 3 | s


(this is an example, there could be more columns or rows), where each cell contains 3 pieces of information: <denominator>|<numerator>|<color>.

<color>: r = red, b = blue, s = standard (black).

How can I put this into a TikZ-matrix applying the information?

Because in TikZ, there are manifold possibilities for visualization, for example:

Left: possible visualization for a quite big table. Right: Another visualization

MWE:

%\documentclass[11pt, landscape]{scrartcl}
\documentclass[border=3mm, varwidth]{standalone}

\usepackage{tikz, amsmath, amssymb}
\usetikzlibrary{matrix}

\begin{document}

IS:
\begin{verbatim}
1 | 3 | s               1 | 1 | r
5 | 3 | b              11 | 3 | b
3 | 1 | r              19 | 3 | s
\end{verbatim}

SHALL: \\
\begin{tikzpicture}
\matrix (m) [
ampersand replacement=\&,
matrix of math nodes,
nodes in empty cells,
]
{%
\frac13 \& |[text=red]|1 \\
|[text=blue]|\frac53 \& |[text=blue]|\frac{11}{3} \\
|[text=red]|3  \&  \frac{19}{3} \\
};
\end{tikzpicture}

\end{document}

• Welcome to TeX.SE! Jan 14, 2018 at 14:58
• Please explain the rule for combining the information in the rows. Perhaps it is the obvious one but it would be good to know - although the colouring is not obvious to me. Are r, b and s tne only labels dor the rows? It would be easier/betterI think if there was a | between the third and fourth columns. Finally, Tikz  does not seem to be necessary from the MWE. Is there some unstated reason as to why it should be used rather than, for example, just a tabular environment?
– user30471
Jan 14, 2018 at 16:25
• Done. ................... Jan 14, 2018 at 16:57
• Please don't put regular text explaining things into code format - screen readers have to read it to users character by character. 'Left: possible visualization for a quite big table. Right: Another visualization' would be easier read as words, don't you think?
– cfr
Jan 15, 2018 at 2:48
• I don't understand the 'Left: possible visualization for a quite big table. Right: Another visualization' comment, though. What is the left bit and what the right bit? And how are they equivalent? And how is that related to what you're trying to do?
– cfr
Jan 15, 2018 at 2:49

Because this question received quite some attention, I'll crosspost my answer to the German version on TeXwelt:

Tikz-Matrix aus speziellem Datensatz erstellen

Using lpeg you can parse arbitrary data formats.

\documentclass{article}

\usepackage{luacode}
\usepackage{xcolor}
\colorlet{s}{black}
\colorlet{b}{blue}
\colorlet{r}{red}

\begin{luacode*}
local lpeg = assert(require"lpeg")
local C, P, R, S = lpeg.C, lpeg.P, lpeg.R, lpeg.S

local space = S(" \t")^0
local line = S("\n\r")^0

local integer = space * R("09")^1 / tonumber
local sep = space * P("|")
local color = space * C(R("az"))

local function col(num, denom, color)
if (denom == 1) then
tex.sprint(string.format("\\textcolor{%s}{$%d$}",color,num))
else
tex.sprint(string.format("\\textcolor{%s}{$\\frac{%d}{%d}$}",color,num,denom))
end
end

local function row()
tex.sprint("\\medbreak")
end

local entry = ((integer * sep * integer * sep * color / col)^1 * line / row)^1

function parse(tab)
entry:match(tab)
end
\end{luacode*}

\begin{document}

\begin{luacode*}
parse[[
1 | 3 | s               1 | 1 | r
5 | 3 | b              11 | 3 | b
3 | 1 | r              19 | 3 | s
]]
\end{luacode*}

\end{document}


Putting everything in a table is left as an exercise. In ConTeXt this is of course much easier...

\definecolor[s][black]
\definecolor[b][blue]
\definecolor[r][red]

\startluacode
local lpeg = assert(require"lpeg")
local C, P, R, S = lpeg.C, lpeg.P, lpeg.R, lpeg.S

local space = S(" \t")^0
local line = S("\n\r")^0

local integer = space * R("09")^1 / tonumber
local sep = space * P("|")
local color = space * C(R("az"))

local function col(num, denom, color)
if (denom == 1) then
context.NC(string.format("\\color[%s]{$%d$}",color,num))
else
context.NC(string.format("\\color[%s]{$\\frac{%d}{%d}$}",color,num,denom))
end
end

local function row()
context.NC()
context.NR()
end

local entry = ((integer * sep * integer * sep * color / col)^1 * line / row)^1

function parse(tab)
context.startTABLE{ frame="off", align="middle,lohi" }
entry:match(tab)
context.stopTABLE()
end
\stopluacode

\starttext

\startluacode
parse[[
1 | 3 | s               1 | 1 | r
5 | 3 | b              11 | 3 | b
3 | 1 | r              19 | 3 | s
]]
\stopluacode

\stoptext