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.

Asked
Active
Viewed 2,248 times
6
-
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 09 '18 at 08:45
3 Answers
9
\MAX defines the maximal width of the bar, which is 1
\documentclass{article}
\usepackage{xcolor}
\newlength\MAX \setlength\MAX{5mm}
\newcommand*\Chart[1]{#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.
3
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}
Bobyandbob
- 4,899
-
1Thank you for your answer. I have finished the table , it's very cool – liang tan Aug 13 '18 at 07:34
2
Here is a solution using tikz:
\documentclass{article}
\usepackage{tikz}
\newcommand{\DrawPercentageBar}[1]{%
\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}
You could also change the dimension of the bar depending on your needs.
Pierpaolo Savina
- 645
-
1You shoulkd use
\newcommand{\DrawPercentageBar}[1]{%and also\end{tikzpicture}%. Then you do not get additional spaces in the output. – Aug 09 '18 at 09:26


