首先我要说的是,我甚至不是 LaTeX 初学者——我几乎一无所知。我有一个名为 title.tex 的 .tex 文件,它是在 Windows 平台上开发的,并在 R markdown 文档中使用,如下所示:
output:
pdf_document:
latex_engine: xelatex
includes:
in_header: "preamble.tex"
before_body: "title.tex"
对于在 Windows 笔记本电脑上创建它的人来说,这很有用,但在我的 Ubuntu 系统上却不行。我得到的输出/错误是:
output file: template_vF.knit.md
/usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS template_vF.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output template_vF.tex --lua-filter /usr/local/lib/R/site-library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /usr/local/lib/R/site-library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --highlight-style tango --pdf-engine xelatex --include-in-header preamble.tex --include-before-body title.tex --variable graphics
! Missing number, treated as zero.
<to be read again>
\vrule
l.138 ...\hspace{25pt}\color{gray}\line(1,0){30pt}
\par
Error: LaTeX failed to compile template_vF.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See template_vF.log for more info.
我注释掉输出中指定的那行,结果又出现了错误。这似乎是语法问题,\hspace
但从我尽我所能检查来看,我似乎正确地使用了该方法。下面的 title.tex 中有什么明显的错误吗?寻求帮助/提示。
title.tex 如下:
\begin{titlepage}
%Change margins for title page
\newgeometry{top=.5cm,bottom=0cm, left=0cm, right = 0cm}
%Import header and footer/but remove footer
\thispagestyle{fancy}
\fancyfoot{}
%Set up left side of page
\begin{minipage}{0.4\textwidth}
%Change to sans serif LaTeX font
\sffamily
%Vertical space between top of page and logo
\vspace*{50pt}
%Indent and insert logo
\hspace{25pt}\includegraphics{logo.png} \par
%Vertical space between logo and Title
\vspace*{150pt}
%Indent and title text
\hspace{25pt}\Huge \textbf{A Huge Title} \par
\vspace*{25pt}
\hspace{25pt} \color{bluefont}\LARGE A large subtitle... \par
\vspace*{1pt}
\hspace{25pt} ...continuing below \par
\vspace*{15pt}
%Divider line
\hspace{25pt}\color{gray}\line(1,0){30pt} \par
%Indent and date
\hspace{25pt}\color{black}\textbf{April 21, 2021}
\end{minipage}%
\hfill
%Set up right half of page
\begin{minipage}[t]{0.6\textwidth}
%Move image anchor to top of header
\vspace*{-200pt}
%Insert cover photo
\includegraphics[width = \textwidth]{cover.jpg}
\vspace{-5pt}
%Change to sans serif LaTeX font
\sffamily
%Create black box below image
%Current height is set to 220pt; increase if new photo causes gap at bottom
%Text and copy right on right hand title
\colorbox{footergray}{\makebox(\textheight,220pt)[l]{\vspace{-50pt}\parbox[l]{\textheight}{\hspace{15pt}\Large \color{white}\textbf{Team Name} \par
\vspace{15pt}\hspace{15pt}\large Person 1 Name \par
\vspace{15pt}\hspace{15pt}\large Person 2 Name \par
\vspace{50pt}
\hspace{.55\textwidth}\copyright Company Name}}}
\end{minipage}
\end{titlepage}
编辑1: 以下是 preamble.tex:
\usepackage[english]{babel}
\usepackage[letterpaper,top=1.25 cm,bottom=1cm,left=0cm,right=0cm,marginparwidth=0cm]{geometry}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{fontspec}
\usepackage{float}
\usepackage{booktabs}
% Define color for top bar
\definecolor{headergray}{RGB}{202, 215, 222}
% Define color for bottom bar
\definecolor{footergray}{RGB}{37, 39, 41}
% Define color for blue font
\definecolor{bluefont}{RGB}{66, 109, 155}
% Define color for table border
\definecolor{bordercolor}{RGB}{21, 147, 134}
% Create top bar colorbox
\usepackage{fancyhdr}
\setlength{\headheight}{20pt}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\fancyhf{}% Remove header/footer
\lhead{\setlength{\fboxsep}{5pt}\colorbox{headergray}{%
\makebox[\dimexpr\linewidth-2pt][l]{\color{headergray}1
\hfill
}%
}}
% Create bottom bar colorbox
\lfoot{\setlength{\fboxsep}{10pt}\colorbox{footergray}{%
\makebox[\dimexpr\linewidth-10pt][l]{\color{white} \sffamily
\hspace{10pt} Footer Left Text
\hfill
\copyright \hspace{3pt} Footer Right Text\hspace{15pt}
\textbf{\thepage} \hspace{15pt}
}
}}
\setlength{\headheight}{0pt}
编辑2:
xelatex --version
XeTeX 3.14159265-2.6-0.999991 (TeX Live 2019/Debian)
kpathsea version 6.3.1
编辑3:(添加 .Rmd - rmarkdown::render("template_mwe.Rmd")
)
---
output:
pdf_document:
latex_engine: xelatex
includes:
in_header: "preamble.tex"
before_body: "title.tex"
documentclass: report
classoption:
- landscape
---
```{=latex}
%Change font style
\setmainfont{Arial}
%Restore templated margins
\restoregeometry
% Add page title and logo to top
\setcounter{page}{2}
\vspace*{10pt}
\hspace{20pt} \Huge Lay of the Land
\large \color{bluefont} Quarterly Figures
\hfill \includegraphics{logo.png} \hspace{20pt}
\vspace*{20pt}
% Set line colors/thickness on tables
\arrayrulecolor{bordercolor}
\setlength{\arrayrulewidth}{1.5pt}
\setlength{\tabcolsep}{0pt}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
options(knitr.kable.NA = "-")
#Import libraries
library(readr)
library(dplyr)
library(magrittr)
library(scales)
library(kableExtra)
library(tidyverse)
```
```{r, echo = FALSE}
### Import data
# f <- file.choose()
mpbqdat <- data.frame(t = rep(c(LETTERS[1:20], letters[1:20]), 4)) %>%
arrange(t) %>%
mutate(
q = rep(c("I", "II", "III", "IV"), 40),
mean = rnorm(160),
med = rnorm(160),
min = rnorm(160),
max = rnorm(160))
```
\fontspec{Arial}
```{r}
#Color cell font/background based on rank in group
color_rows <- function(x) {
sgn <- (x > 0)*1 + 1
rnk <- rank(x, ties.method = "first", na.last = "keep")
cell_spec(x, align = "c",
color = case_when(is.na(sgn) ~ "black",
TRUE ~ c("#ff1342", "#126667")[sgn]),
background = case_when(is.na(rnk) ~ "white",
TRUE ~ c("#ffc8d1", "#ffe4e8", "#edfdfb", "#68eadd")[rnk])
)
}
#Select and color mean q
meanq <-
mpbqdat %>%
select(t, q, mean) %>%
pivot_wider(names_from = q,
values_from = mean) %>%
column_to_rownames("t") %>%
mutate(across(everything(), function(x) scales::percent(x, accuracy = .01))) %>%
apply(., 1, color_rows)
#Select and color median q
medianq <-
mpbqdat %>%
select(t, q, med) %>%
pivot_wider(names_from = q,
values_from = med) %>%
column_to_rownames("t") %>%
mutate(across(everything(), function(x) scales::percent(x, accuracy = .01))) %>%
apply(., 1, color_rows)
#Select and color max q
maxQtr <- mpbqdat %>%
select(t, q, max) %>%
pivot_wider(names_from = q,
values_from = max) %>%
select(-t) %>%
mutate(across(everything(), function(x) scales::percent(x, accuracy = .01))) %>%
apply(., 1, color_rows)
#Select and color min q
minQtr <- mpbqdat %>%
select(t, q, min) %>%
pivot_wider(names_from = q,
values_from = min) %>%
select(-t) %>%
mutate(across(everything(), function(x) scales::percent(x, accuracy = .01))) %>%
apply(., 1, color_rows)
#Create double spacer column for output spacing
blankRow <- rep("",ncol(meanq))
blankRow <- rbind(blankRow, blankRow)
#Combine qs with spacer rows for final output
output <- rbind(meanq,blankRow)
output <- rbind(output,medianq)
output <- rbind(output,blankRow)
output <- rbind(output, maxQtr)
output <- rbind(output, blankRow)
output <- rbind(output, minQtr)
output <- rbind(output, blankRow)
#Rename columns based on qs
rownames(output) = c("I", "II","III","IV", "",
"", "I", "II","III","IV", "",
"", "I", "II","III","IV", "",
"", "I", "II","III","IV","",""
)
#Add spacer rows between ts at given freq
freq <- 12
skips <- floor(ncol(output)/freq)
for (i in 1:skips) {
output <- cbind(output[,1:(freq*i+(i-1))],"",output[,(freq*i+i):ncol(output)])
}
#Add spacer rows at top and bottom of output
output <- cbind(output[,0],"",output[,1:ncol(output)])
output <- cbind(output[,1:ncol(output)],"")
#Convert output to list to be able to use in Kable
tablePrep <- list(
output %>%
t()
)
#Create Kable contruct
kable(tablePrep, format = "latex",
escape = FALSE,
longtable = FALSE,
booktabs = TRUE,
linesep = "") %>%
#Create custom styling on final outputted table
#font_size - change font size across whole table
kable_styling(font_size = 6.75,
full_width = FALSE,
latex_options = "HOLD_position", position = "center") %>%
#add_header_above - add/format header for q labels
add_header_above(c(" "=1, "q" = 4, " "=2, "q" = 4, " "=2, "q" = 4, " "=2, "q" = 4," "=2), color = "#0E635A", align = "l", bold = TRUE) %>%
#add_header_above - add/format header for mean/med/min/max labels
add_header_above(c(" "=1, "AVERAGE RETURN" = 4, " "=2, "MEDIAN RETURN" = 4, " "=2, "MAX RETURN" = 4, " "=2, "MIN RETURN" = 4, " "=2),background = c("white","#558A9B"), color = "white", line = FALSE, align = "l", bold = TRUE) %>%
#row_spec - format I/II/III/IV header row
row_spec(0, align = "c", bold = TRUE, color = "#558A9B", hline_after = FALSE)%>%
#Remove default horizontal lines running full length of table
sub("\\\\toprule", "", .) %>%
sub("\\\\midrule", "", .) %>%
sub("\\\\bottomrule", "", .) %>%
#Set width of t column
column_spec(1, width = "1.4cm")%>%
#Set width of data columns and left borders
column_spec(c(2:5,8:11,14:17,20:23), width = "1.46cm") %>%
column_spec(c(2,8,14,20), width = "1.46cm", border_left = TRUE)%>%
#Set right borders via dummy spacer columns
column_spec(c(6,12,18,24), width_min = "0pt", width = "0pt", border_right = TRUE) %>%
#Set width of spacer columns
column_spec(c(7,13,19), width = ".6cm") %>%
#Add bottom borders
row_spec(ncol(output), extra_latex_after = "\\cline{2-5} \\cline{8-11} \\cline{14-17} \\cline{20-23}")%>%
#Add top borders
row_spec(0, extra_latex_after = "\\cline{2-5} \\cline{8-11} \\cline{14-17} \\cline{20-23}")
```
编辑4:(由 R markdown 生成 TeX) 由于篇幅过长,已上传至 Google Drive
答案1
解决方案
我xelatex
遇到了与原始帖子相同的错误。
第一步是重建mwe.tex
第二步原文有错别字:
老的:
\colorbox{footergray}{\makebox(\textheight,220pt)[l]
{\vspace{-50pt}\parbox[l]{\textheight}{\hspace{15pt}\Large
\color{white}\textbf{Team Name} \par
新的:
\colorbox{footergray}{\makebox{\textheight,220pt}[l]
\vspace{-50pt}\parbox[l]{\textheight}{\hspace{15pt}\Large
\color{white}\textbf{Team Name} \par
拼写错误是\makebox
,此标记的参数应放在大括号中{ ... }
,而不是圆括号中 ( ... )
原文中还有其他需要挑剔的地方,但与当前的问题无关。
上面概述的差异应该可以帮你解决。
您所追踪的错误发生在文档的更上层。
环境
$ xelatex --version
XeTeX 3.14159265-2.6-0.99998 (TeX Live 2017/Debian)
kpathsea version 6.2.3
平均能量损失
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{fontspec}
\usepackage{float}
\usepackage{booktabs}
\usepackage[english]{babel}
\usepackage[letterpaper,
top=1.25 cm,
bottom=1cm,
left=0cm,
right=0cm,
marginparwidth=0cm]{geometry}
\definecolor{headergray}{RGB}{202, 215, 222}
\definecolor{footergray}{RGB}{37, 39, 41}
\definecolor{bluefont}{RGB}{66, 109, 155}
\definecolor{bordercolor}{RGB}{21, 147, 134}
% Create top bar colorbox
\usepackage{fancyhdr}
\setlength{\headheight}{20pt}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\fancyhf{}% Remove header/footer
\lhead{\setlength{\fboxsep}{5pt}\colorbox{headergray}{%
\makebox[\dimexpr\linewidth-2pt][l]{\color{headergray}1
\hfill
}%
}}
% Create bottom bar colorbox
\lfoot{\setlength{\fboxsep}{10pt}\colorbox{footergray}{%
\makebox[\dimexpr\linewidth-10pt][l]{\color{white} \sffamily
\hspace{10pt} Footer Left Text
\hfill
\copyright \hspace{3pt} Footer Right Text\hspace{15pt}
\textbf{\thepage} \hspace{15pt}
}
}}
\setlength{\headheight}{0pt}
\begin{document}
\begin{titlepage}
\newgeometry{top=.5cm,bottom=0cm, left=0cm, right = 0cm}
\thispagestyle{fancy}
\fancyfoot{}
\begin{minipage}{0.4\textwidth}
\sffamily
\vspace*{50pt}
\hspace{25pt}\includegraphics{logo.png} \par
\vspace*{150pt}
\hspace{25pt}\Huge \textbf{A Huge Title} \par
\vspace*{25pt}
\hspace{25pt} \color{bluefont}\LARGE A large subtitle... \par
\vspace*{1pt}
\hspace{25pt} ...continuing below \par
\vspace*{15pt}
\hspace{25pt}
\color{gray}
\line(1,0) {30}
\par
\hspace{25pt}\color{black}\textbf{April 21, 2021}
\end{minipage}
\hfill
\begin{minipage}[t]{0.6\textwidth}
\vspace*{-200pt}
\includegraphics[width = \textwidth]{cover.jpg}
\vspace{-5pt}
\sffamily
\colorbox{footergray}{%
\makebox{\textheight,220pt}[l]{%
\vspace{-50pt}\parbox[l]{%
\textheight}{%
\hspace{15pt}\Large \color{white}\textbf{Team Name} \par
\vspace{15pt}\hspace{15pt}\large Person 1 Name \par
\vspace{15pt}\hspace{15pt}\large Person 2 Name \par
\vspace{50pt}
\hspace{0.55\textwidth}
\copyright\ Company Name}}}
\end{minipage}
\end{titlepage}
\end{document}
答案2
虽然上面的答案帮助了我并教会了我一些新的东西,但我在 10 小时后通过改变解决了这个问题
%Divider line
\hspace{25pt}\color{gray}\line(1,0){30pt} \par
到
%Divider line
\hspace{25pt}\color{gray}\line(1,0){30} \par