tufte-book 类和 R 列表代码框宽度问题

tufte-book 类和 R 列表代码框宽度问题

当我运行以下 MWE 时:

\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%


%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%

\begin{document}

\begin{Schunk}
\begin{rexample}\label{set1}\hfill{}\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}\end{Schunk}

\begin{Schunk}
\begin{rexample}\label{set2}\hfill{}\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}\end{Schunk}

\end{document}

包含 R 输入和输出代码,产生以下输出

使用 knitr 编译 LaTeX 输出

请注意,输入框的宽度与输出框的宽度不同。如何使它们的宽度相等?

此代码需要Sweavel.sty在此处获取sweavel.sty在包含 LaTeX 和 R 代码的 .Rnw 文件上运行 knir 时生成

答案1

Routstyle您需要重新定义列表样式的定义。

在您使用的文件中,Sweavel.sty它定义为

\lstdefinestyle{Routstyle}{%
  fancyvrb=false,
  literate={~}{{$\sim$}}1{R^2}{{$R^{2}$}}2{^}{{$^{\scriptstyle\wedge}$}}1{R-squared}{{$R^{2}$}}2,%
  frame=single, 
  framerule=0.2pt,
  framesep=1pt,
  basicstyle=\Routcolor\Routsize\ttfamily,%
  backgroundcolor=\Routbackground}

您可以在序言中使用以下代码:

\lstdefinestyle{Routstyle}{%
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
}

因此,完整的代码如下

\documentclass{tufte-book}

\usepackage{Sweavel}

\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{soul}

%%%%%%%%%%
% R example
%%%%%%%%%%
\newtheoremstyle{rex}{}{}{}{.45\textwidth}{}{}{1 mm}{}
\theoremstyle{rex}
\newtheorem{rexample}{R example}[chapter]

%%%%%%%%%%
% boxes
%%%%%%%%%%
\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{style2}{
    backgroundcolor=gray!10,
}
\usepackage[strict]{changepage}
%%%%%%%%%%

%%%%%%%%%%
% crossref
%%%%%%%%%%
\usepackage[capitalize,noabbrev]{cleveref}
    \crefname{rexample}{Example}{Examples}%
%%%%%%%%%%

%%%%%%%%%%
% layout
%%%%%%%%%%
\newcommand{\blankpage}{\newpage\hbox{}\thispagestyle{empty}\newpage}
%%%%%%%%%%


%%%%%%%%%%
% listings
%%%%%%%%%%
\newcommand{\indexfunction}[1]{\index{#1@\texttt{#1}}}
\usepackage{listings}
\lstset{language = R,
  frame = single,
}
\def\Rcolor{\color{black}}
\def\Routcolor{\color{black}}
\def\Rcommentcolor{\color{red}}
\def\Rbackground{\color[rgb]{0.992, 0.965, 0.894}}
\def\Routbackground{\color[rgb]{0.894, 0.965, 0.992}}
\definecolor{lightYellow}{rgb}{0.992, 0.965, 0.894}
\sethlcolor{lightYellow}
%%%%%%%%%%

\lstdefinestyle{Routstyle}{% <==========================================
  language=R,%
  basicstyle={\Routcolor\Sweavesize\ttfamily},
  backgroundcolor=\Routbackground,%
} % <===================================================================
%%%%%%%%%%
% book metadata
%%%%%%%%%%
\title[]{}
\author[]{}
\publisher{}
%%%%%%%%%%


\begin{document}

\begin{Schunk}
\begin{rexample}
\label{set1}\hfill{}
\begin{Sinput}
setS <- letters[1:5] %>% print()
\end{Sinput}
\begin{Soutput}
[1] "a" "b" "c" "d" "e"
\end{Soutput}
\end{rexample}
\end{Schunk}

\begin{Schunk}
\begin{rexample}
\label{set2}\hfill{}
\begin{Sinput}
"e" %in% setS
\end{Sinput}
\begin{Soutput}
[1] TRUE
\end{Soutput}
\begin{Sinput}
"f" %in% setS
\end{Sinput}
\begin{Soutput}
[1] FALSE
\end{Soutput}
\end{rexample}
\end{Schunk}

\end{document}

结果如下:

生成的 pdf

相关内容