2

I'll ask my question here since I think it has more to do with LaTeX than stackoverflow. I am using R Markdown with LaTeX to create pdf documents that shows some tables. Using the R language with Markdown and LaTeX is a bit tricky. I currently want to display a table similar to this one :

Age         Womens         Mens          Total
1: [20,30)    179,0 (2,4)    118,0 (2,0)    297,0 (2,2)
2: [30,40) 2 325,0 (30,9) 2 115,0 (35,4) 4 440,0 (32,9)
3: [40,80) 1 814,0 (24,1) 1 610,0 (26,9) 3 424,0 (25,4)
...

The LaTeX version of this table have the booktabs and striped options.

The problem is that when I knitr (run the code), [ is causing the following error :

! Paragraph ended before \CT@rowd was complete. \par

I know this is the problem since when I changes [ by ( I got no problem and my table is showing up without any problems but I have to use it for this document [.

The R package I use for LaTeX is kableExtra which convert my table into LaTeX code (which gives an error), the code with a problem is :

\begin{table}[!h]
\centering
\begin{tabular}{llll}
\toprule
Age & Womens & Mens & Total\\
\midrule
\rowcolor{gray!6}  [65,70) & 179,0 (2,4) & 118,0 (2,0) & 297,0 (2,2)\\
[70,75) & 2 325,0 (30,9) & 2 115,0 (35,4) & 4 440,0 (32,9)\\
\rowcolor{gray!6}  [75,80) & 1 814,0 (24,1) & 1 610,0 (26,9) & 3 424,0 (25,4)\\
[80,85) & 1 226,0 (16,3) & 939,0 (15,7) & 2 165,0 (16,0)\\
\rowcolor{gray!6}  [85,90) & 1 033,0 (13,7) & 745,0 (12,5) & 1 778,0 (13,2)\\
\addlinespace
[90,95) & 609,0 (8,1) & 343,0 (5,7) & 952,0 (7,1)\\
\rowcolor{gray!6}  [95,100) & 253,0 (3,4) & 96,0 (1,6) & 349,0 (2,6)\\
[100,109) & 82,0 (1,1) & 15,0 (0,3) & 97,0 (0,7)\\
\rowcolor{gray!6}  Total & 7 521,0 (100,0) & 5 981,0 (100,0) & 13 502,0 (100,0)\\
\bottomrule
\end{tabular}
\end{table}

Anyone knows why [ isn't making my table displaying? I already searched here and on stack without finding similar questions. Also [ doesn't seems to be an escaping character in LaTeX. Any help would be appreciated, thank you.

Edit :

Age Womens Mens Total
[65,70) 179,0 (2,4) 118,0 (2,0) 297,0 (2,2)
 [70,75) 2 325,0 (30,9) 2 115,0 (35,4) 4 440,0 (32,9)
[75,80) 1 814,0 (24,1) 1 610,0 (26,9) 3 424,0 (25,4)
 [80,85) 1 226,0 (16,3) 939,0 (15,7) 2 165,0 (16,0)
[85,90) 1 033,0 (13,7) 745,0 (12,5) 1 778,0 (13,2)
 [90,95) 609,0 (8,1) 343,0 (5,7) 952,0 (7,1)
[95,100) 253,0 (3,4) 96,0 (1,6) 349,0 (2,6)
 [100,109) 82,0 (1,1) 15,0 (0,3) 97,0 (0,7)
Total 7 521,0 (100,0) 5 981,0 (100,0) 13 502,0 (100,0)

1 Answer 1

3

Try like this:

Edited for the correction of alignment of first column ... But sure there is a better way..

\begin{table}[!h]
\centering
\begin{tabular}{@{}llll}
\toprule
{ }Age & Womens & Mens & Total\\
\midrule
\rowcolor{gray!6}{ }[65,70) & 179,0 (2,4) & 118,0 (2,0) & 297,0 (2,2)\\{}
[70,75) & 2 325,0 (30,9) & 2 115,0 (35,4) & 4 440,0 (32,9)\\
\rowcolor{gray!6}  { }[75,80) & 1 814,0 (24,1) & 1 610,0 (26,9) & 3 424,0 (25,4)\\{}
[80,85) & 1 226,0 (16,3) & 939,0 (15,7) & 2 165,0 (16,0)\\
\rowcolor{gray!6}  { }[85,90) & 1 033,0 (13,7) & 745,0 (12,5) & 1 778,0 (13,2)\\
\addlinespace{}
[90,95) & 609,0 (8,1) & 343,0 (5,7) & 952,0 (7,1)\\
\rowcolor{gray!6}  { }[95,100) & 253,0 (3,4) & 96,0 (1,6) & 349,0 (2,6)\\{}
[100,109) & 82,0 (1,1) & 15,0 (0,3) & 97,0 (0,7)\\
\rowcolor{gray!6} {} Total & 7 521,0 (100,0) & 5 981,0 (100,0) & 13 502,0 (100,0)\\
\bottomrule
\end{tabular}
\end{table}

Explanation: I added some {} in order that LaTeX will realize that the upcomming [ is not starting an optional argument for the vertical space that would be added (or removed) after the command \\ (like \\[+1cm] etc)

8
  • Hi, thanks a lot for your answer. Your code seems to be good but I still get the same error. Could a LaTeX package cause such errors?
    – Gainz
    Aug 27, 2019 at 18:10
  • Are you sure you copy-pasted the code as it is? If yes... please add a MWE (tex.meta.stackexchange.com/q/228/120578) that will give this error with my code...
    – koleygr
    Aug 27, 2019 at 18:21
  • Yes I copy-pasted the code as it is, but it didn't work since R need one more \ to escape the character, so I added \ and it still didn't work. I'll take a look at your link, thank you.
    – Gainz
    Aug 27, 2019 at 18:25
  • 1
    Thanks @Gainz ... I will ask a question later if nobody add another answer here and will update my answer if we find the "correct" way
    – koleygr
    Aug 27, 2019 at 18:54
  • 1
    Thanks a lot! I'll be looking too!
    – Gainz
    Aug 27, 2019 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .