harrelfe / Hmisc

Harrell Miscellaneous
Other
205 stars 81 forks source link

latex(..., ctable = TRUE, booktabs = TRUE) does not compile correctly in LaTeX #99

Closed bheebsh closed 5 years ago

bheebsh commented 5 years ago

I ran into an issue with the combination of ctable and booktabs packages in the latex() function.

Example R Code

library(Hmisc)

lor<- c(.2362)
se<- c(.1234)
summary.stats<- cbind(
    'Log Odds Ratio'=lor, 
    'Standard Error'=se

)

summary.stats

w <- 
latex(summary.stats, 
    cdec=c(3, 3), col.just=rep('c',2),
    rowname=c('Death (all cause)'),
    rowlabel='', caption='Statistical Results',
    ctable=TRUE, booktabs = TRUE,  
        file = 'ctable_booktabs.tex')

This produces the following .tex file which doesn't compile.

\documentclass{article}[]

\usepackage{booktabs}
\usepackage{ctable}

\begin{document}

\ctable[caption={},label=summary.stats,pos=!tbp,]{lcc}{}{

\toprule             

\multicolumn{1}{l}{}&\multicolumn{1}{c}{Log Odds Ratio}&\multicolumn{1}{c}{Standard Error}\midrule                 

Death (all cause)&$0.236$&$0.123$\bottomrule

}

\end{document}

The problem is with the \midrule and \bottomrule parts, which require preceding newlines. This isn't an issue with ctable = FALSE option, because both are preceded by \tabularnewline in that case.

The issue (as far as I can tell) is only present when using the combination.

I am getting this on R 3.5.1 and Hmisc 4.1.1.

I am happy to branch and take stab at fixing it myself and putting in a pull request.

harrelfe commented 5 years ago

You are welcome to have a go at it. I'd like Thomas Dupont to give his input at some point.

bheebsh commented 5 years ago

Thanks, Prof. Harrel.

I was able to fix it relatively straightforwardly by adding logic to check for the combination when setting the end-of-group-operators. I traced it back to the latex.s file, lines 404-433.

In the CRAN version, the end-of-group line is always "" if ctable is in use, regardless of whether booktabs is used. I simply added a condition to use a normal newline if both ctable and booktabs are in use and leave it as-is otherwise.