2

I'm aligning a range of numbers on an en-dash between two columns in a tabular. I'd like to add color to alternating groups of rows, but using \cellcolor covers up the en-dash.

Note: this tabular is ultimately nested into one column of a larger tabular, so I haven't had much luck trying to use \rowcolor instead.

\documentclass{article}
\usepackage[table]{xcolor}
\newcommand\cc{\cellcolor[gray]{.7}}

\begin{document}
\begin{tabular}{r@{--}l}
1 & 10 \\
\cc 11 & \cc 50 \\
51 & 1,000 \\
\end{tabular}
\end{document}
  • No need to load colortbl if you also load xcolor with the table option. – leandriis Jan 10 at 21:02
  • Ah, thanks. Thought my MWE was M enough already… – Sue Dunham Jan 10 at 21:10
2

Sorry the user interface isn't great, one simple version is

enter image description here

\documentclass{article}
\usepackage[table]{xcolor}
\newcommand\cc{\cellcolor[gray]{.7}}

\begin{document}
\setlength\tabcolsep{0pt}
\begin{tabular}{r>{--}l}
1 & 10 \\
\cc 11 & \cc 50 \\
51 & 1,000 \\
\end{tabular}
\end{document}
1

You may use the excellent (and fast) dcolumn package. Since a dcolumn is in math mode, you need icomma or similar package to avoid a space after the thousands separator. You also need to adjust the width of the column to compensate for the space the comma occupies. I added a small tabcolsep:

If you prefer \textendash instead of maths minus sign, just repalce the second hyphen with the command \textendash, eventually with some space before and after:

\newcolumntype{d}{D{-}{\textendash}{2,4}}

THe example below is set in maths mode.

enter image description here

\documentclass{article}
\usepackage{dcolumn, array, icomma}
\usepackage[table]{xcolor}
\newcommand\cc{\cellcolor[gray]{.7}}

\newcolumntype{d}{D{-}{-}{2,4}}

\begin{document}

\setlength\tabcolsep{2pt}
\begin{tabular}{d}
1-10 \\
\cc11-50 \\
51-1,000 \\
\end{tabular}
\end{document}
  • I've been playing with this solution, and I was wondering if there was a way to change the math mode minus sign to an en dash, specifically using unicode-math. It's pretty close as is, but coming here has made me sensitive to the distinction! – Sue Dunham Jan 13 at 18:21
  • @SueDunham See updated answer. – Sveinung Jan 13 at 18:56

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.