4

Hi I am quite new to latex, but loving it so far. I am using Rmarkdown with xtable to create a table with a graph at the end.

A question was asked two years ago - Is it possible to create a barchart in a table? - on how to create a barchart inside a table. It worked perfectly.

My question: Can I put a number after the graph?

What I am looking for:
enter image description here

If that is not possible then:
enter image description here

A solution by A.Ellett looked like this: enter image description here
And the solution was:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array}
\usepackage{xcolor}
\def\mybar#1{%%
  #1s & {\color{red}\rule{#1cm}{8pt}}}

\pagestyle{empty}
\begin{document}

\begin{tabular}{>{$\rhd$ }lrl}
Loop at line 151 in divergence  & \mybar{3.420}\\
Loop at line 1071 in radiation  & \mybar{3.270}\\
scalar face value               & \mybar{3.090}\\
Loop at line 102 in get         & \mybar{1.700}\\
get sensible enthalpy           & \mybar{1.250}\\
\end{tabular}

\end{document}

Here he adds a new column with the values. Xtable had a problem with this extra column due to aligning. Can this be done without the extra column?

My solution so far:
The latex header:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array}
\usepackage{xcolor}
\definecolor{blueColor}{rgb}{0,.30667,.55111}
\def\mybar#1{%%
 {\color{blueColor}\rule{#1mm}{9pt}}} %I only want the bar, not the extra column

The xtable solution:

align <- paste0(c("L{4cm}", rep("C{15mm}", 4), "L{3cm}"), collapse = "")
latexGraph <- function(x) gsub('GRAPH(.*)',paste('\\\\mybar{\\1}'),x)

print(xtable(x, align=align, auto = TRUE),
  sanitize.text.function = latexGraph) #To add the graph to the table

How my function works now is: I have a column with numbers for example (1, 2, 3, 4, 5). To change those numbers to a graph, I change these numbers to: GRAPH1, GRAPH2, GRAPH3, GRAPH4, GRAPH5. Xtable then uses my function (latexGraph) to 'sanatize' this column. The function changes the GRAPH4 to \mybar{\4}.

  • What do you mean with a number behind the graph? Like the '3.420s' inside the colored bar?? If so I think using tikz is the easiest way. – Guilherme Zanotelli Sep 19 '16 at 07:07
  • Preferably I would like the numbers behind the bar (see edit to question). But if that is not possible, yes then inside the bar. How would one go about doing that with tikz? – Helgi Guðmundsson Sep 19 '16 at 07:17

1 Answers1

3

Following what you said in the comments, that is way easier, you just have to change the order from the \mybar command:

\def\mybar#1{%%
  {\color{red}\rule{#1cm}{8pt}} #1s} % See the #1s repeating itself?

The MWE looks like this:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array}
\usepackage{xcolor}
\def\mybar#1{%%
  {\color{red}\rule{#1cm}{8pt}} #1s}

\pagestyle{empty}
\begin{document}

\begin{tabular}{>{$\rhd$ }ll}
Loop at line 151 in divergence  & \mybar{3.420}\\
Loop at line 1071 in radiation  & \mybar{3.270}\\
scalar face value               & \mybar{3.090}\\
Loop at line 102 in get         & \mybar{1.700}\\
get sensible enthalpy           & \mybar{1.250}\\
\end{tabular}

\end{document}

And the result, like this:

enter image description here

But if you are typesetting a lot of numbers I would recomend that you use the siunitx package which provides a huge amount of tools for typesetting numbers and units, here is a MWE with siunitx (the command \sisetup sets the Format of all numbers and unit processed by siunitx):

\documentclass{article}
\usepackage{amsmath,amssymb,siunitx}
\sisetup{output-decimal-marker={,}, round-mode=places, round-precision=2}
\usepackage{array}
\usepackage{xcolor}
\def\mybar#1{%%
  {\color{red}\rule{#1cm}{8pt}} \SI{#1}{\second}}

\pagestyle{empty}
\begin{document}

\begin{tabular}{>{$\rhd$ }ll}
Loop at line 151 in divergence  & \mybar{3.420}\\
Loop at line 1071 in radiation  & \mybar{3.270}\\
scalar face value               & \mybar{3.090}\\
Loop at line 102 in get         & \mybar{1.700}\\
get sensible enthalpy           & \mybar{1.250}\\
\end{tabular}

\end{document}

The result is pretty much the same but now the numbers follow the format specified in \sisetup, also there is the thin space between unit and number.

enter image description here

  • Wow worked perfectly! Your solution even works with the R package Xtable without any hickups. Thank you so much for your help. – Helgi Guðmundsson Sep 19 '16 at 08:02
  • No problem whatsoever! It was a rather simple solution. And I don't know the purpose of your document, but check the siunitx package fot typesetting numbers, on the begining it might seem too much work, but later on it eases on you and open a great number of possibilities for number and unit formatting. The manual is also nicely written. – Guilherme Zanotelli Sep 19 '16 at 08:07
  • Thanks for that. I'll definitely check out the package. I can see the benefit in using it. – Helgi Guðmundsson Sep 19 '16 at 08:24