【问题标题】:change the title color Rmd file更改标题颜色 Rmd 文件
【发布时间】:2019-10-03 13:51:21
【问题描述】:

我正在尝试更改生成 Pdf 的 Rmd 文件中标题的颜色

我试过这个(带引号和不带引号):

---
title: "\textcolor{blue}{This title is blue}"
output:
  pdf_document:
  latex_engine: xelatex
---

# 1. TITLE 1

## 1.1 Subtitle

会出现这种错误:

错误:无法编译 test_font.tex。`

然后我尝试了这个:

---
title: <div class="blue">This title is blue</div>
output:
  pdf_document:
  latex_engine: xelatex
---

# 1. TITLE 1

## 1.1 Subtitle

不会改变颜色

有什么建议吗? 谢谢!

【问题讨论】:

    标签: r pdf fonts colors r-markdown


    【解决方案1】:

    我发现的最简单的方法是使用 html 标记。下面的例子。

    ---
    title: <span style="color:blue">R Notes</span>
    output: html_document
    ---
    
    Hello world!
    ```{r Hello world}
    "hello world"
    ```
    

    【讨论】:

      【解决方案2】:

      我找到了一种通过执行以下操作来更改从 RMarkdown 文件编译的 PDF 标题颜色的方法。此过程不需要对中间 TeX 文件进行任何手动修改。

      我有一个包含 LaTeX 代码的头文件,用于导入颜色包并定义一些自定义颜色,它被命名为header.tex

      \usepackage{color}
      \definecolor{NavyBlue}{RGB}{0,112,192}
      

      我的 RMarkdown 文件的 YAML 部分在 ouput 部分中包含以下行。

      output: 
        pdf_document:
          includes:
            in_header: header.tex
          keep_tex: true
          toc: false
      

      title 行如下。

      title: \textcolor{NavyBlue}{`r paste0('Report - ', params$Location)`}
      

      生成的标题如下所示。

      如果您使用的是 TinyTeX,则无需担心下载任何包。

      【讨论】:

        【解决方案3】:

        我怀疑是否有办法使这项工作开箱即用(经过一番搜索,我找不到,但很高兴被证明是错误的)。我可以为你解释为什么这不起作用。

        首先,要使用\textcolor{}{},您需要color 包。因此,尝试不使用该软件包进行编译每次都会失败。

        所以,你可能会(天真地)说,让我们包含颜色包:

        ---
        output: pdf_document
        header-includes:
           - \usepackage{color}
        title: \textcolor{blue}{This text is blue}
        ---
        
        # 1. TITLE 1
        
        ## 1.1 Subtitle
        

        但是,您仍然会遇到编译错误。因此,我检查了生成的.tex 文件,并看到以下内容:

        ... [output omitted]
        
        \setlength{\droptitle}{-2em}
        
          \title{\textcolor{blue}{This text is blue}}
            \pretitle{\vspace{\droptitle}\centering\huge}
          \posttitle{\par}
            \author{}
            \preauthor{}\postauthor{}
            \date{}
            \predate{}\postdate{}
        
        \usepackage{color}
        ... [output omitted]
        

        R Markdown 会将标题包含在 标题信息之后,这将再次不起作用。您可以手动将其编辑为以下内容:

        ... [output omitted]
        
        \usepackage{color} % Include *before* using \textcolor{}{}
        
        \setlength{\droptitle}{-2em}
        
          \title{\textcolor{blue}{This text is blue}}
            \pretitle{\vspace{\droptitle}\centering\huge}
          \posttitle{\par}
            \author{}
            \preauthor{}\postauthor{}
            \date{}
            \predate{}\postdate{}
        ... [output omitted]
        

        成功编译您的文档:

        但这对于 R Markdown 文档来说似乎是不必要的复杂,它应该让你的生活更轻松

        我可能会建议在 https://github.com/rstudio/rmarkdown 提交问题,看看这是否会改变未来用户的行为。

        【讨论】:

        • 哇哦。你的意思是在编织后编辑 .tex 文件?好吧,这听起来确实不好玩。非常感谢您的回答,启发,我会提出一个问题。
        猜你喜欢
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 2019-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多