[英]* in tabular LaTeX table displaying error in Rmarkdown
I am trying to create a LaTeX table using Rmarkdown.我正在尝试使用 Rmarkdown 创建一个 LaTeX 表。 The table seems to working fine in LaTeX, but when I set it up on Rmarkdown, it is throwing up the following error:该表在 LaTeX 中似乎工作正常,但是当我在 Rmarkdown 上设置它时,它会抛出以下错误:
processing file: table_5.Rmd
output file: table_5.knit.md
! File ended while scanning use of \@argdef.
<inserted text>
\par
<*> table_5.tex
I think the issue maybe arissing from the \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
where both the curly brackets are asterisk are used together.我认为问题可能来自\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
,其中两个大括号都是星号一起使用。 Could anyone suggest a way to go around this issue in Rmarkdown?任何人都可以在 Rmarkdown 中围绕这个问题提出 go 的方法吗?
The code to reproduce the issue is as follows:重现该问题的代码如下:
---
output:
pdf_document:
latex_engine: lualatex
html_document: default
word_document: default
header-includes:
- \usepackage[utf8]{inputenc}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \usepackage{booktabs}
- \usepackage{array}
- \usepackage{threeparttable}
- \usepackage{lscape}
- \usepackage{caption}
- \usepackage{rotating}
- \newcommand\mytabC[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
- \newcommand\mytabL[1]{\hspace{5mm}\begin{tabular}{@{}l@{}} #1 \end{tabular}}
---
\clearpage
\renewcommand\arraystretch{1.2}
\begin{sidewaystable}
\setlength\tabcolsep{0pt}
\scriptsize
\begin{threeparttable}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare diam erat, quis accumsan massa congue bibendum. }
\label{tab:my-tab5}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
\toprule
&
\multicolumn{2}{c}{Model 1} &
\multicolumn{2}{c}{Model 2} &
\multicolumn{2}{c}{Model 3} &
\multicolumn{2}{c}{Model 4} \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
Occupational class &
Coefficient\tnote{a} & 95\% CI\tnote{b} &
Coefficient\tnote{a} & 95\% CI &
Coefficient & 95\% CI\tnote{b} &
Coefficient\tnote{a} & 95\% CI\tnote{b} \\
\midrule
\multicolumn{5}{@{}l}{\textbf{Father's occupational class at age 5}} & \\
Female ($N=2{,}980$) & \\
\mytabL{I Professional/II Intermediate\\ III Skilled (non-manual\ or manual)\\ IV Partly skilled/V Unskilled} &
\mytabC{0.00 (ref)\\ 0.004\\ 0.00} &
\mytabC{—\\ 0.009, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& <0.001; <0.001 & & 0.025; 0.025 & & 0.003; 0.004 & & 0.002; 0.002 \\
\addlinespace
Female ($N=2{,}925$) & \\
\mytabL{I Professional/II Intermediate\\ III Skilled (non-manual\ or manual)\\ IV Partly skilled/V Unskilled} &
\mytabC{0.00 (ref)\\ -0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.36\\ 1.01}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.743; 0.568 & & 0.123; 0.045 & & 0.411; <0.001 & & 0.261; 0.408 \\
\addlinespace
\multicolumn{5}{@{}l}{\textbf{Mother's highest qualification at age 5}} & \\
Female ($N=2{,}485$) & \\
\mytabL{Vocational/degree and higher\\ A-level/equiv.\\ \hspace{2mm}Vocational/O-level/equiv.\\ No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& <0.001; <0.001 & & <0.001; 0.008 & & <0.001; 0.002 & & 0.005; 0.001 \\
\addlinespace
Female ($N=2{,}412$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 1.46} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.304; 0.064 & & 072; 0.046 & & 0.160; 0.305 & & 0.078; — \\
\addlinespace
\multicolumn{5}{@{}l}{\textbf{Father's highest qualification at age 5}} \\
Female ($N=2{,}343$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ -0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ -0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.304; 0.064 & & 0.899; 0.698 & & 0.715; 0.368 & & 0.642; 0.287 \\
\addlinespace
Female ($N=2{,}286$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.002\\ 0.00} &
\mytabC{—\\ 0.00, 0.002\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.08\\ 0.00\\ -0.08} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.80\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.123; 0.020 & & 0.003; <0.001 & & 0.056; 0.009 & & 0.986; 0.866 \\
\bottomrule
\end{tabular*}
\smallskip
\begin{tablenotes}
\item[a] Duis leo ante, malesuada sit amet magna at, dictum feugiat dui;
\item[b] CI: Confidence Interval;
\item[c] P-value: p-value from a likelihood ratio test of the overall association;
\item[d] P-trend: p-value not presented for relationship where there was significant deviation from linearity
\end{tablenotes}
\end{threeparttable}
\end{sidewaystable}
\end{document}
The problem is that rmarkdown is not clever enough to correctly parse the #
in your custom command.问题是 rmarkdown 不够聪明,无法正确解析自定义命令中的#
。 You can avoid the problem by hiding the definitions in a.tex file and input this file:您可以通过将定义隐藏在 a.tex 文件中并输入此文件来避免此问题:
---
output:
pdf_document:
latex_engine: lualatex
keep_tex: true
html_document: default
word_document: default
header-includes:
- \usepackage[utf8]{inputenc}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \usepackage{booktabs}
- \usepackage{array}
- \usepackage{threeparttable}
- \usepackage{lscape}
- \usepackage{caption}
- \usepackage{rotating}
- \input{preamble}
---
\clearpage
\renewcommand\arraystretch{1.2}
\begin{sidewaystable}
\setlength\tabcolsep{0pt}
\scriptsize
\begin{threeparttable}
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ornare diam erat, quis accumsan massa congue bibendum. }
\label{tab:my-tab5}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{8}{c} }
\toprule
&
\multicolumn{2}{c}{Model 1} &
\multicolumn{2}{c}{Model 2} &
\multicolumn{2}{c}{Model 3} &
\multicolumn{2}{c}{Model 4} \\
\cmidrule{2-3} \cmidrule{4-5} \cmidrule{6-7} \cmidrule{8-9}
Occupational class &
Coefficient\tnote{a} & 95\% CI\tnote{b} &
Coefficient\tnote{a} & 95\% CI &
Coefficient & 95\% CI\tnote{b} &
Coefficient\tnote{a} & 95\% CI\tnote{b} \\
\midrule
\multicolumn{5}{@{}l}{\textbf{Father's occupational class at age 5}} & \\
Female ($N=2{,}980$) & \\
\mytabL{I Professional/II Intermediate\\ III Skilled (non-manual\ or manual)\\ IV Partly skilled/V Unskilled} &
\mytabC{0.00 (ref)\\ 0.004\\ 0.00} &
\mytabC{—\\ 0.009, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& <0.001; <0.001 & & 0.025; 0.025 & & 0.003; 0.004 & & 0.002; 0.002 \\
\addlinespace
Female ($N=2{,}925$) & \\
\mytabL{I Professional/II Intermediate\\ III Skilled (non-manual\ or manual)\\ IV Partly skilled/V Unskilled} &
\mytabC{0.00 (ref)\\ -0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.36\\ 1.01}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.743; 0.568 & & 0.123; 0.045 & & 0.411; <0.001 & & 0.261; 0.408 \\
\addlinespace
\multicolumn{5}{@{}l}{\textbf{Mother's highest qualification at age 5}} & \\
Female ($N=2{,}485$) & \\
\mytabL{Vocational/degree and higher\\ A-level/equiv.\\ \hspace{2mm}Vocational/O-level/equiv.\\ No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& <0.001; <0.001 & & <0.001; 0.008 & & <0.001; 0.002 & & 0.005; 0.001 \\
\addlinespace
Female ($N=2{,}412$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\multicolumn{1}{c}{\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00}} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 1.46} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.304; 0.064 & & 072; 0.046 & & 0.160; 0.305 & & 0.078; — \\
\addlinespace
\multicolumn{5}{@{}l}{\textbf{Father's highest qualification at age 5}} \\
Female ($N=2{,}343$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ -0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ -0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.304; 0.064 & & 0.899; 0.698 & & 0.715; 0.368 & & 0.642; 0.287 \\
\addlinespace
Female ($N=2{,}286$) & \\
\mytabL{Vocational/degree and higher\\
A-level/equiv.\\
Vocational/O-level/equiv.\\
No qualification} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.002\\ 0.00} &
\mytabC{—\\ 0.00, 0.002\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.00\\ 0.00\\ 0.00} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.00\\ 0.00, 0.00} &
\mytabC{0.00 (ref)\\ 0.08\\ 0.00\\ -0.08} &
\mytabC{—\\ 0.00, 0.00\\ 0.00, 0.80\\ 0.00, 0.00} \\
\hspace{2.5mm}$p$-values (overall\tnote{c}; test for trend\tnote{d}) &
& 0.123; 0.020 & & 0.003; <0.001 & & 0.056; 0.009 & & 0.986; 0.866 \\
\bottomrule
\end{tabular*}
\smallskip
\begin{tablenotes}
\item[a] Duis leo ante, malesuada sit amet magna at, dictum feugiat dui;
\item[b] CI: Confidence Interval;
\item[c] P-value: p-value from a likelihood ratio test of the overall association;
\item[d] P-trend: p-value not presented for relationship where there was significant deviation from linearity
\end{tablenotes}
\end{threeparttable}
\end{sidewaystable}
with preamble.tex
:与preamble.tex
:
\newcommand\mytabC[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}
\newcommand\mytabL[1]{\hspace{5mm}\begin{tabular}{@{}l@{}} #1 \end{tabular}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.