在 rmarkdown 中使用 \newenvironment
Using \newenvironment in rmarkdown
我正在尝试在Rmarkdown文档中创建一个密码本风格的环境,如下所示:
---
title: "Title"
author: "Author"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: yes
fig_crop: yes
keep_tex: no
number_sections: yes
citation_package: biblatex
html_document:
df_print: paged
header-includes:
- \usepackage[english]{babel}
- \usepackage[utf8]{inputenc}
- \usepackage{amsmath}
- \usepackage{graphicx}
- \usepackage{caption}
- \usepackage{physics}
- \usepackage{float}
- \usepackage{tocloft}
- \usepackage{inputenc}
- \usepackage{setspace}
- \usepackage{tabularx}
- \usepackage{lipsum}
- \usepackage{listings}
- \usepackage{wrapfig}
- \usepackage{mathtools}
- \usepackage{lscape}
- \usepackage{rotating}
- \usepackage{epstopdf}
- \usepackage{hyperref}
- \geometry{letterpaper, portrait, margin=1in}
- \hypersetup{colorlinks = false, linkbordercolor = {white}, citebordercolor = {white},
urlbordercolor = {white}}
- \lstset{language=R, basicstyle=\small\ttfamily,stringstyle=\color{DarkGreen},otherkeywords={0,1,2,3,4,5,6,7,8,9},morekeywords={TRUE,FALSE},deletekeywords={data,frame,length,as,character}, keywordstyle=\color{blue}, commentstyle=\color{DarkGreen}}
fontsize: 14pt
---
\pagenumbering{arabic}
\doublespacing
\newpage
Defining function here:
\newlength\cbl
\newenvironment{codebook}[1][]{
\settowidth{\cbl}{#1}
\parskip1em plus .3em minus .2em
\parindent0pt
\def\code##1##2{{\bfseries ##1}\hfill
\parbox[t]{\dimexpr\linewidth-15em-\cbl}{##2}\par}}{\noindent}
\newpage
\singlespacing
Testing out the environment here:
\subsubsection{Codebook}
\begin{codebook}
\code{country}{Country name.}
\code{year}{Year of country observation.}
\code{codename}{Country code (character).}
\code{codenumber}{Country code (numeric).}
\end{codebook}
我已确认 \newenvironment
代码在 TeXstudio 中正常工作,但是当我尝试将此文档编织成 PDF 时,出现以下错误:
! Undefined control sequence.
<argument> \code
我假设 \newenvironment
命令中的某些内容不能很好地与 rmarkdown 语法一起使用。任何帮助将不胜感激。
如果您在 YAML 中设置 keep_tex: yes
,您可以获得关于哪里出了问题的提示。从 \subsubsection{Codebook}
开始,您会看到
\subsubsection{Codebook}
\settowidth{\cbl}{\code}
\parskip 1em plus .3em minus .2em
\parindent 0pt
\def\code#\code#
所以发生的事情是 Pandoc 试图处理
您的环境定义,而不是让它单独存在。您可以像这样输入它来强制它忽略定义:
```{=latex}
\newlength\cbl
\newenvironment{codebook}[1][rob\_avprison1]{
\settowidth{\cbl}{#1}
\parskip1em plus .3em minus .2em
\parindent0pt
\def\code##1##2{{\bfseries ##1}\hfill
\parbox[t]{\dimexpr\linewidth-15em-\cbl}{##2}\par}}{\noindent}
```
这在您的最小示例中似乎就足够了,但您可能需要在真实文档中包含更多内容。
我正在尝试在Rmarkdown文档中创建一个密码本风格的环境,如下所示:
---
title: "Title"
author: "Author"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: yes
fig_crop: yes
keep_tex: no
number_sections: yes
citation_package: biblatex
html_document:
df_print: paged
header-includes:
- \usepackage[english]{babel}
- \usepackage[utf8]{inputenc}
- \usepackage{amsmath}
- \usepackage{graphicx}
- \usepackage{caption}
- \usepackage{physics}
- \usepackage{float}
- \usepackage{tocloft}
- \usepackage{inputenc}
- \usepackage{setspace}
- \usepackage{tabularx}
- \usepackage{lipsum}
- \usepackage{listings}
- \usepackage{wrapfig}
- \usepackage{mathtools}
- \usepackage{lscape}
- \usepackage{rotating}
- \usepackage{epstopdf}
- \usepackage{hyperref}
- \geometry{letterpaper, portrait, margin=1in}
- \hypersetup{colorlinks = false, linkbordercolor = {white}, citebordercolor = {white},
urlbordercolor = {white}}
- \lstset{language=R, basicstyle=\small\ttfamily,stringstyle=\color{DarkGreen},otherkeywords={0,1,2,3,4,5,6,7,8,9},morekeywords={TRUE,FALSE},deletekeywords={data,frame,length,as,character}, keywordstyle=\color{blue}, commentstyle=\color{DarkGreen}}
fontsize: 14pt
---
\pagenumbering{arabic}
\doublespacing
\newpage
Defining function here:
\newlength\cbl
\newenvironment{codebook}[1][]{
\settowidth{\cbl}{#1}
\parskip1em plus .3em minus .2em
\parindent0pt
\def\code##1##2{{\bfseries ##1}\hfill
\parbox[t]{\dimexpr\linewidth-15em-\cbl}{##2}\par}}{\noindent}
\newpage
\singlespacing
Testing out the environment here:
\subsubsection{Codebook}
\begin{codebook}
\code{country}{Country name.}
\code{year}{Year of country observation.}
\code{codename}{Country code (character).}
\code{codenumber}{Country code (numeric).}
\end{codebook}
我已确认 \newenvironment
代码在 TeXstudio 中正常工作,但是当我尝试将此文档编织成 PDF 时,出现以下错误:
! Undefined control sequence.
<argument> \code
我假设 \newenvironment
命令中的某些内容不能很好地与 rmarkdown 语法一起使用。任何帮助将不胜感激。
如果您在 YAML 中设置 keep_tex: yes
,您可以获得关于哪里出了问题的提示。从 \subsubsection{Codebook}
开始,您会看到
\subsubsection{Codebook}
\settowidth{\cbl}{\code}
\parskip 1em plus .3em minus .2em
\parindent 0pt
\def\code#\code#
所以发生的事情是 Pandoc 试图处理 您的环境定义,而不是让它单独存在。您可以像这样输入它来强制它忽略定义:
```{=latex}
\newlength\cbl
\newenvironment{codebook}[1][rob\_avprison1]{
\settowidth{\cbl}{#1}
\parskip1em plus .3em minus .2em
\parindent0pt
\def\code##1##2{{\bfseries ##1}\hfill
\parbox[t]{\dimexpr\linewidth-15em-\cbl}{##2}\par}}{\noindent}
```
这在您的最小示例中似乎就足够了,但您可能需要在真实文档中包含更多内容。