Rmd to PDF compiling error: Package geometry \paperwidth (0.0pt) too short

Rmd to PDF compiling error: Package geometry \paperwidth (0.0pt) too short

我正在用 R markdown 写一篇论文,需要用学术期刊提供的 this .cls file 格式化它。

最小的 .tex 文件可以与上述 cls 文件完美编译。

我的 .tex 文件(在 ShareLaTeX 上编译, clv3.cls 保存在同一目录中):

\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}

\title{Paper title}
\author{Name Surname}
\date{May 2018}

\begin{document}

\maketitle

\section{Introduction}

Some text.

\end{document}

然而,在 R markdown 中使用相同 cls 文件的类似最小文档无法在 Rstudio 中编译,并出现以下错误:! Package geometry Error: \paperwidth (0.0pt) too short.

我的 Rmd 文件(与 clv3.cls 文件保存在同一目录):

---
title: "Paper title"
author: "Name Surname"
documentclass: clv3
classoption: shortpaper
output: pdf_document
---

# Introduction

Some text.

当我尝试将此 class 文件与 R markdown 文档一起使用时,为什么会出现此错误?我该如何解决?

我试过在 YAML header 中手动指定页宽设置,但我真的不知道自己在做什么。无论如何,这似乎是不可取的,因为正常的 LaTeX 文档在没有它的情况下也能正常工作(当然页面宽度应该由期刊指定,而不是由作者手动覆盖)。

我不知道 clv3.cls class 和默认 pandoc 模板冲突的确切位置。但是,该模板做了很多事情,在使用特定样式编写时没有意义,最好使用您自己的模板。使用 clv3-template.tex

\documentclass[shortpaper]{clv3}
\usepackage[utf8]{inputenc}

$if(title)$
  \title{$title$}
$else$
  \title{}
$endif$
$if(author)$
  \author{$for(author)$$author$$sep$ \ $endfor$}
$else$
  \author{}
$endif$

\begin{document}

$if(title)$
\maketitle
$endif$

$body$

\end{document}

连同

---
title: "Paper title"
author: "Name Surname"
output: 
  pdf_document:
    template:
      clv3-template.tex
---

# Introduction

Some text.

应该是一个很好的起点。

接受的答案非常适合所提供的最小示例。然而,随着文档变得更加复杂(例如,插入参考书目和文内引用),它很快就会再次中断。我想对我的解决方案进行一些扩展,以便将来的读者受益,因为我发现它的学习曲线有点陡峭:

这里的问题是 Pandoc 有一个用于生成 PDF 文档的 LaTeX 模板。这与定义文档 class 的 .cls class 文件是分开的。正如 Ralf Stubner 所说,关于我的特定 class 文件的某些内容没有与 Pandoc 的默认模板合作。这对许多人来说可能是非常基本和显而易见的,但我没有欣赏这个额外的步骤,也没有理解这些文件之间的区别。

如果不想处理原始 LaTeX,似乎有很多模板可用于各种文档(例如,参见 rticle 包)。然而,使用 R Markdown 以特定自定义格式(例如特定期刊)生成 PDF 文档将需要构建 LaTeX 模板。这可以通过以下两种方式之一完成:

  1. 修补现有模板,直到获得所需的模板,方法是找到 Pandoc 的默认模板并从那里开始(请参阅 user2554330 的评论了解位置)或通过在 Github 上克隆其他人的模板等。
  2. 从头开始编写模板。在这种情况下,上面 Ralf Stubner 的最小示例加上 Pandoc 手册的 this section 将提供信息。

就我而言,我选择了后一种选择。我已将最终模板保存为 an R package,可以使用 devtools::install_github("JaydenM-C/CLtemplate") 安装。因此,如果其他人想使用这种特定的文档样式为 计算语言学 编写文档,这可能会节省您一些时间。