6

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. enter image description here

  • 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
7

\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}

enter image description here

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:

enter image description here

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}
  • 1
    Thank you for your answer. I have finished the table , it's very cool – liang tan Aug 13 '18 at 7: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}

Here is the result:enter image description here

You could also change the dimension of the bar depending on your needs.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.