如何在 R Markdown 中使用 Latex 动态着色页面的一侧

问题描述 投票:0回答:1

我正在尝试使用 R Markdown 和 Latex 准备纸质模板。我一边学习一边学习,对我来说下一个挑战是能够动态地对页面的侧面进行着色。换句话说,我希望能够对具有奇数页码的页面右侧进行着色,并且我希望能够对具有偶数页码的页面左侧进行着色。到目前为止,我已经找到了一种能够不断为页面右侧着色的解决方案。这是到目前为止我的 .Rmd 代码;

---
title: ""
author: ""
abstract: ""
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      in_header: customized_paper_template.tex
date: "2024-06-29"
urlcolor: blue
linkcolor: red
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{=tex}
\begin{titlepage}
    \raggedleft
    \rule{1pt}{\textheight} 
    \hspace{0.05\textwidth} 
    \parbox[b]{0.75\textwidth}{
        {\Huge\bfseries Paper Template}\\[1\baselineskip] 
        {\large \today}\\[2\baselineskip] 
        {\large{Abstract}}\\
        {\large{This paper works on XXX}}\\[6\baselineskip]
        {\Large\textsc{Author Name}}\\
        \vspace{0.5\textheight}\\
        {\includegraphics[width=5cm]{logo.png}}
    }
\end{titlepage}
```
```{=tex}
\newpage
\pagenumbering{arabic}
\renewcommand\contentsname{Contents}

\newgeometry{a4paper, total={170mm,257mm}, left=20mm, top=20mm,     bottom=20mm, right=50mm}
\definecolor{light}{HTML}{13294B}
\definecolor{highlight}{HTML}{800080}
\definecolor{dark}{HTML}{330033}
```
```{=tex}
\AddToShipoutPicture{
    \AtPageLowerLeft{ 
        \put(\LenToUnit{\dimexpr\paperwidth-3cm},0){
            \color{light}\rule{3cm}{\LenToUnit\paperheight}
          }
     }
%    \AtPageLowerLeft{
%        \put(\LenToUnit{\dimexpr\paperwidth-2.7cm},27.2cm){
%            \color{dark}\includegraphics[width=2.5cm].   {logo.png}
%          }
%     }
}
\def\UrlFont{\urlfont}
\def\LinkFont{\linkfont}
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

## Including Plots

You can also embed plots, for example: This is an R Markdown     document. Markdown is a simple formatting syntax for authoring     HTML, PDF, and MS Word documents. For more details on using R     Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:Note that the `echo = FALSE` parameter was     added to the code chunk to prevent printing of the R code that     generated the plot.

You can also embed plots, for example: This is an R Markdown     document. Markdown is a simple formatting syntax for authoring     HTML, PDF, and MS Word documents. For more details on using R     Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:Note that the `echo = FALSE` parameter was     added to the code chunk to prevent printing of the R code that     generated the plot.

### Including Extra Plots

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

This is an R Markdown document. Markdown is a simple formatting     syntax for authoring HTML, PDF, and MS Word documents. For more     details on using R Markdown see <http://rmarkdown.rstudio.com>.     When you click the **Knit** button a document will be generated     that includes both content as well as the output of any     embedded R code chunks within the document. You can embed an R     code chunk like this:

customized_paper_template.tex
文件的内容是这样的;

\usepackage{fontspec}
\usepackage{geometry}
\geometry{a4paper, margin=1in} % Default geometry for the title     page
\usepackage{setspace}
\usepackage{xcolor}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{sectsty}

\renewcommand{\baselinestretch}{1.25}

% Set body text font
\setmainfont{Kepler Std Light Semicondensed}

% Set title fonts
\newfontfamily\titlefont{Tiempos Headline}

% Set hyperlink fonts
\newfontfamily\urlfont{Roboto Slab}
\newfontfamily\linkfont{Roboto Slab}


% Set the font size for the document
\AtBeginDocument{\fontsize{12pt}{14pt}\selectfont}

% Define title formatting
\usepackage{titlesec}
 \titleformat{\section}
  {\normalfont\LARGE\titlefont} % Title style
  {\thesection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
   [{\titlerule[0.5pt]}] % Add a line under the title with     0.5pt thickness

\titleformat{\subsection}
  {\normalfont\LARGE\titlefont} % Title style
  {\thesubsection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [{\titlerule[0.5pt]}]  % Add a line under the title with     0.5pt thickness

\titleformat{\subsubsection}
  {\normalfont\Large\titlefont} % Title style
  {\thesubsubsection} % Section number
  {1em} % Space between number and title
  {} % No additional formatting before the title text
  [{\titlerule[0.5pt]}]  % Add a line under the title with     0.5pt thickness


% Add headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{} % clear all header and footer fields
\fancyhead[L]{Course Name} % section title on the left
\fancyhead[R]{Author Name} % fixed text on the right
\fancyfoot[C]{\thepage} % page number in the center of the     footer  
% Remove header line
\renewcommand{\headrulewidth}{0pt}  

生成的 pdf 输出也是这样的;

enter image description here

我更喜欢一个解决方案,我可以将其写入customized_paper_template.tex 文件中,这样主.Rmd 文件就不会变得混乱。提前感谢您的建议。

r pdf latex r-markdown xelatex
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.