使用包列表的代码风格不起作用

使用包列表的代码风格不起作用

在本文档中,我想使用两种代码样式,DOS 和 R。我已使用包分别定义了这两种样式listings。DOS 样式效果很好,但 R 样式无法正确着色线条。

代码(已测试 MWE 并且正常运行)

\documentclass{article}
\usepackage[paper=a4paper]{geometry}% http://ctan.org/pkg/geometry
\font\titlefont=cmr12 at 40pt

\usepackage{verbatim}
\usepackage[utf8]{inputenc}
%\usepackage{xcolor}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color} % Important for r script boxes

\lstdefinestyle{DOS}
{
    backgroundcolor=\color{black},
    basicstyle=\scriptsize\color{white}\ttfamily
}

\lstdefinestyle{R}
{
    basicstyle=\small\ttfamily,
  numbers=left,                   % where to put the line-numbers
  backgroundcolor=\color{white},  % choose the background color
  frame=single,                   % frame around code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text
  tabsize=1,                      % sets default tabsize
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{Blue},      % keyword style
  commentstyle=\color{Green},     % comment style
  stringstyle=\color{ForestGreen} % string literal style
}


\usepackage{hyperref}
\hypersetup{
     colorlinks   = true,
     linkcolor = blue
}

\usepackage{natbib}
\usepackage{graphicx}

\usepackage{afterpage}
\usepackage{xcolor}

\usepackage{epigraph}
\setlength\epigraphwidth{.8\textwidth}
\setlength\epigraphrule{0pt}

%\title{\titlefont ML Police Python}

\title{%
  \titlefont Title \\
  \lineskip 0.3em
  \large Smaller title \\}
\author{by \\ 
Me}
\date{June 2018}

\begin{document}
\pagenumbering{gobble}% Remove page numbers (and reset to 1)

\pagecolor{yellow}\afterpage{\nopagecolor}
\maketitle

\vspace*{\fill}

\epigraph{\itshape Quote}{--- By a famous person}

\newpage
\newgeometry{top=1in,bottom=1in,right=1.5in,left=1.5in}
\pagenumbering{arabic}% Arabic page numbers (and reset to 1)


\noindent Activate python shell with the command 'python' and hit enter:

\begin{lstlisting}[style=DOS]
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2018 Microsoft Corporation. All rights reserved.

c:\Users\Superman>python
Python 2.7.2 (v2.7.2:f59c0932b4, Mar 28 2010, 16:07:46) [MSC v. 1900 32 bit Intel] on win32
Type "help", "copyright", "credits" or "license" for more information
>>>
\end{lstlisting}

\newpage

\topskip0pt
\vspace*{\fill}
\begin{center}
\color{white}{
  \normalsize\bfseries\MakeUppercase{\titlefont EXTRA}
  }
\end{center}
\vspace*{\fill}

\pagecolor{purple}\afterpage{\nopagecolor}

\newpage

\section*{R-code version}

\begin{lstlisting}[style=R]
# data processing 
library(sp)
library(plyr)
library(dplyr)
library(dummies)
library(caret)

### STEP 1: Pre-process dataset.

# import main dataset
data = read.csv("data.csv")

\end{lstlisting}

\end{document}

答案1

您的代码中存在一些问题:

  • hyperref应该最后调用
  • color可以省略的包。使用xcolor具有相同选项的包
  • 添加缺失language=R\lstdefinestyle{R}

请参阅以下 MWE(重要的代码更改以 标记<========

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage[paper=a4paper]{geometry}% http://ctan.org/pkg/geometry
\font\titlefont=cmr12 at 40pt

\usepackage{verbatim}
\usepackage[usenames,dvipsnames]{xcolor} % Important for r script boxes <============================
\usepackage{listings}

\lstdefinestyle{DOS}
{
    backgroundcolor=\color{black},
    basicstyle=\scriptsize\color{white}\ttfamily
}

\lstdefinestyle{R}
{
  language=R,                     % <===================================
  basicstyle=\small\ttfamily,
  numbers=left,                   % where to put the line-numbers
  backgroundcolor=\color{white},  % choose the background color
  frame=single,                   % frame around code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text
  tabsize=1,                      % sets default tabsize
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{Blue},      % keyword style
  commentstyle=\color{Green},     % comment style
  stringstyle=\color{ForestGreen} % string literal style
}

\usepackage{natbib}
\usepackage{graphicx}

\usepackage{afterpage}

\usepackage{epigraph}
\setlength\epigraphwidth{.8\textwidth}
\setlength\epigraphrule{0pt}

\usepackage{hyperref} % <===============================================
\hypersetup{
     colorlinks   = true,
     linkcolor = blue
}

%\title{\titlefont ML Police Python}

\title{%
  \titlefont Title \\
  \lineskip 0.3em
  \large Smaller title \\}
\author{by \\ 
Me}
\date{June 2018}




\begin{document}
\pagenumbering{gobble}% Remove page numbers (and reset to 1)

\pagecolor{yellow}\afterpage{\nopagecolor}
\maketitle

\vspace*{\fill}

\epigraph{\itshape Quote}{--- By a famous person}

\newpage
\newgeometry{top=1in,bottom=1in,right=1.5in,left=1.5in}
\pagenumbering{arabic}% Arabic page numbers (and reset to 1)


\noindent Activate python shell with the command 'python' and hit enter:

\begin{lstlisting}[style=DOS]
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2018 Microsoft Corporation. All rights reserved.

c:\Users\Superman>python
Python 2.7.2 (v2.7.2:f59c0932b4, Mar 28 2010, 16:07:46) [MSC v. 1900 32 bit Intel] on win32
Type "help", "copyright", "credits" or "license" for more information
>>>
\end{lstlisting}

\newpage

\topskip0pt
\vspace*{\fill}
\begin{center}
\color{white}{
  \normalsize\bfseries\MakeUppercase{\titlefont EXTRA}
  }
\end{center}
\vspace*{\fill}

\pagecolor{purple}\afterpage{\nopagecolor}

\newpage

\section*{R-code version}

\begin{lstlisting}[style=R]
# data processing 
library(sp)
library(plyr)
library(dplyr)
library(dummies)
library(caret)

### STEP 1: Pre-process dataset.

# import main dataset
data = read.csv("data.csv")

\end{lstlisting}

\end{document}

以及结果页面

结果页面

没有错误消息和警告...

相关内容