如何用Quarto正确渲染双面美观PDF?
问题描述
Quarto文档使用scrreprt文档类,设置classoption: [oneside]时可正常渲染,但切换为classoption: [twoside]后出现渲染异常,自定义的侧边栏、边距和页码样式无法适配双面模式。
解决方案
针对双面模式的适配需求,需调整LaTeX代码中的边距设置、侧边栏位置及页码布局,以下是修改后的完整代码:
--- book: title: "Title" author: "MYaseen208" format: pdf: documentclass: scrreprt classoption: [twoside] toc: true toc-depth: 3 lot: true lof: true include-in-header: - text: | %% 加载依赖包 \usepackage{lipsum} \usepackage{geometry} \usepackage{xcolor} \usepackage{eso-pic} \usepackage{fancyhdr} \usepackage{sectsty} \usepackage{fontspec} \usepackage{titlesec} %% 双面模式下设置内外边距(inner=内侧边距,outer=外侧边距) \geometry{a4paper, total={170mm,257mm}, inner=20mm, outer=50mm, top=20mm, bottom=20mm} %% 定义颜色变量 \definecolor{light}{HTML}{E6E6FA} \definecolor{highlight}{HTML}{800080} \definecolor{dark}{HTML}{330033} %% 双面模式下交替设置侧边栏:奇数页右侧,偶数页左侧 \AddToShipoutPicture{% \ifodd\value{page}% \AtPageLowerLeft{% \put(\LenToUnit{\dimexpr\paperwidth-3cm},0){% \color{light}\rule{3cm}{\LenToUnit\paperheight}% }% }% \else% \AtPageLowerLeft{% \put(0,0){% \color{light}\rule{3cm}{\LenToUnit\paperheight}% }% }% \fi% } include-before-body: - text: | %% 双面模式页码样式:奇数页右下,偶数页左下 \fancypagestyle{mystyle}{ \fancyhf{} \renewcommand\headrulewidth{0pt} \fancyfoot[RO,LE]{\thepage} \fancyfootoffset{3.5cm} } \setlength{\footskip}{20pt} %% 启用自定义页码样式 \pagestyle{mystyle} %% 章节字体样式设置 \chapterfont{\color{dark}\fontsize{20}{16.8}\selectfont} \sectionfont{\color{dark}\fontsize{18}{16.8}\selectfont} \subsectionfont{\color{dark}\fontsize{14}{16.8}\selectfont} %% 左对齐标题 \makeatletter \renewcommand{\maketitle}{\bgroup\setlength{\parindent}{0pt} \begin{flushleft} {\sffamily\huge\textbf{\MakeUppercase{\@title}}} \vspace{0.3cm} \\ {\Large {\@subtitle}} \\ \@author \end{flushleft}\egroup } \makeatother --- # Introduction \lipsum[1-50] # Material and Methods \lipsum[1-20]
修改要点说明
- 边距适配:将原固定的
left/right边距改为inner(内侧)和outer(外侧),匹配双面模式下左右页边距交替的规则。 - 侧边栏位置:通过
\ifodd\value{page}判断页码奇偶性,奇数页在右侧添加侧边栏,偶数页在左侧添加,保证双面布局的一致性。 - 页码布局:设置页码为
\fancyfoot[RO,LE]{\thepage},即奇数页右下(Right Odd)、偶数页左下(Left Even),符合双面文档的阅读习惯。 - 样式生效:添加
\pagestyle{mystyle}确保自定义页眉页脚样式全局应用。
内容的提问来源于stack exchange,提问作者MYaseen208
相关产品推荐
相关产品推荐

