所以,我在一个项目中,用户可以选择一个选项,并且输出 pdf 应相应生成为 pdf 报告。 Shiny 应用程序运行良好,pdf 报告也运行良好。我想更进一步,自定义 pdf 报告的样式,使其在视觉上更具吸引力。为此,我几乎使用了我在网上找到的类似的定制 pdf 样式。但是,我很难将用
.tex
编写的自定义 pdf 样式应用于用 RMarkdown
制作的动态 pdf 报告。所以,这是 R Markdown 文档中的示例代码;
---
title: "`r unique(data_selected$aaa)` - AAA"
subtitle: BBB
geometry: margin=1in
output:
pdf_document:
latex_engine: xelatex
fig_caption: true
keep_tex: true
includes:
in_header: pdf_report_styling.tex
---
# Header 1
some text here
`{r aaa}
data <- read_excel("data.xlsx")
data_selected <- data %>%
filter(aaa == data$aaa)
table_gt <- data_selected %>%
gt() %>%
tab_style(style = list(cell_fill(color = "lightgray")),
locations = cells_body(columns = everything())
)
table_gt
我用来自定义 pdf 报告的
.tex
文件是这样的;
% load packages
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{sectsty}
\usepackage{fontspec}
\usepackage{titlesec}
%% Set page size with a wider right margin
\geometry{a4paper, total={170mm,257mm}, left=20mm, top=20mm, bottom=20mm, right=50mm}
%% Let's define some colours
\definecolor{light}{HTML}{D3D3D3}
\definecolor{highlight}{HTML}{800080}
\definecolor{dark}{HTML}{330033}
%% Let's add the border on the right hand side
\AddToShipoutPicture{%
\AtPageLowerLeft{%
\put(\LenToUnit{\dimexpr\paperwidth-3cm},0){%
\color{light}\rule{3cm}{\LenToUnit\paperheight}%
}%
}%
% logo
}
%% Style the page number
\fancypagestyle{mystyle}{
\fancyhf{}
\renewcommand\headrulewidth{0pt}
\fancyfoot[R]{\thepage}
\fancyfootoffset{3.5cm}
}
\setlength{\footskip}{20pt}
%% style the chapter/section fonts
\chapterfont{\color{dark}\fontsize{20}{16.8}\selectfont}
\sectionfont{\color{dark}\fontsize{20}{16.8}\selectfont}
\subsectionfont{\color{dark}\fontsize{14}{16.8}\selectfont}
\titleformat{\subsection}
{\sffamily\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]
% left align title
\makeatletter
\renewcommand{\maketitle}{\bgroup\setlength{\parindent}{0pt}
\begin{flushleft}
{\sffamily\huge\textbf{\MakeUppercase{\@title}}} \vspace{0.3cm} \newline
{\Large {\@subtitle}} \newline
\@author
\end{flushleft}\egroup
}
\makeatother
%% Use some custom fonts
\setsansfont{Ubuntu}[
Path = fonts/Ubuntu/,
Scale = 0.9,
Extension = .ttf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
]
\setmainfont{keplerstd}[
Path = fonts/keplerstd/,
Scale = 0.9,
Extension = .ttf,
UprightFont = garr45w,
BoldFont = bold,
ItalicFont = italic
]
尝试生成动态pdf报告时出现以下错误;
! LaTeX Error: There's no line here to end.
我感谢任何解决问题的意见和建议。感谢您事先的关注。
您的设置存在许多问题:
在您的
flushleft
环境中,您发出 \newline
,这就是问题的原因(“这里没有结束线。”)。如果您想要结构化布局,最好将内容设置在tabular
内。例如:
\begin{tabular}{@{} l @{}}
\sffamily\huge\bfseries\MakeUppercase{\@title}\strut \\
\Large\strut\@subtitle \\
\@author
\end{tabular}%
注意使用
\strut
来使用当前字体大小的“最大”元素。
或者,在设置新的文本行之前,在行尾使用 \par
。
但是,上面的方法仍然不起作用。仅仅因为您在 YAML 标头中使用
title
、subtitle
(和 author
)并不一定意味着它们将在 (La )TeX。事实上,唯一可用的默认标题元素是 \@title
、\@subtitle
(和 \@author
)...除非您使用提供该功能的文档类(如 KOMA-script)。