# How to use bar chart to represent percentage in table?

I want to use black and gray bar to represent some percentages. Gray is the basic color on bottom,the black is the real percentage. • Welcome to TeX.SX! Intresting question. What have you done so far? Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. – Bobyandbob Aug 9 '18 at 8:45

\MAX defines the maximal width of the bar, which is 1

\documentclass{article}
\usepackage{xcolor}
\newlength\MAX  \setlength\MAX{5mm}
\newcommand*\Chart{#1~\rlap{\textcolor{black!20}{\rule{\MAX}{2ex}}}\rule{#1\MAX}{2ex}}
\begin{document}

\begin{tabular}{@{} l l l @{}}
Pull Up Method & \Chart{1.000} & \Chart{0.600}\\
Move Field     & \Chart{0.269} & \Chart{0.783}
\end{tabular}

\end{document} It is also possible to change the height of the bar to the height of the characters.

Alternativly you could create a single bar like this: Is it possible to create a barchart in a table?. With \usepackage{calc} you could compute the differnce between the max value (defined with \newlength\WIDTHOFBAR and \setlength\WIDTHOFBAR{1cm}) to get the percentages representation with the following definiton.

Bar chart definition:

\def\blackwhitebar#1{%%
#1 {\color{black!100}\rule{#1cm}{8pt}}{\color{black!30}\rule{\WIDTHOFBAR - #1 cm}{8pt}}}


Solution: MWE:

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{calc}

\newlength\WIDTHOFBAR
\setlength\WIDTHOFBAR{1cm}

\def\blackwhitebar#1{%%
#1 {\color{black!100}\rule{#1cm}{8pt}}{\color{black!30}\rule{\WIDTHOFBAR - #1 cm}{8pt}}}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ l r r r r r }
\toprule
& \multicolumn{2}{c}{A} & \multicolumn{2}{c}{B}\\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
Type & Precision & Recall  & Precision & Recall \\\midrule
Move Type & \blackwhitebar{1.000}  &\blackwhitebar{0.968} & ... & ... \\
Extract Type & \blackwhitebar{1.000}  &\blackwhitebar{0.600} & ... & . \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

• Thank you for your answer. I have finished the table , it's very cool – liang tan Aug 13 '18 at 7:34

Here is a solution using tikz:

\documentclass{article}

\usepackage{tikz}

\newcommand{\DrawPercentageBar}{%
\begin{tikzpicture}
\fill[color=black]   (0.0 , 0.0) rectangle (#1*3ex , 1.5ex );
\fill[color=gray] (#1*3ex  , 0.0) rectangle (3.0ex, 1.5ex);
\end{tikzpicture}%
}

\begin{document}

0.800 \DrawPercentageBar{0.8}

\end{document}


Here is the result: You could also change the dimension of the bar depending on your needs.

• You shoulkd use \newcommand{\DrawPercentageBar}{% and also \end{tikzpicture}%. Then you do not get additional spaces in the output. – user2478 Aug 9 '18 at 9:26