R markdown 在rmarkdown中使用\newenvironment
我正在尝试在Rmarkdown文档中创建代码簿样式的环境,如下所示:R markdown 在rmarkdown中使用\newenvironment,r-markdown,newenvironment,R Markdown,Newenvironment,我正在尝试在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:
---
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#
所以发生的事情是,潘多克试图处理
您的环境定义,而不是将其单独放置。通过如下方式输入定义,可以强制它忽略定义:
```{=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}
```
在您的示例中,这似乎足够了,但您可能需要在实际文档中包装更多内容。谢谢!这适用于最低限度的示例和完整的文档。非常感谢。