阴影区域保持黑色

阴影区域保持黑色

我正在上一门课程。在创建问题集时,代码运行良好,我将阴影区域设为灰色,其中包含 R 代码。这是在那里运行的代码:

 \documentclass[]{book}
    
    %These tell TeX which packages to use.
    \usepackage{array,epsfig}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{amsxtra}
    \usepackage{amsthm}
    \usepackage{mathrsfs}
    \usepackage{color}
    \usepackage[shortlabels]{enumitem}
    \usepackage{multicol}
    \usepackage{adjustbox}
    \usepackage{listings}
    \usepackage[svgnames]{xcolor}
    \usepackage{hyperref}
    \usepackage{framed}
    \definecolor{shadecolor}{named}{Gainsboro}
    \usepackage{fancyhdr}

\begin{shaded}

\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},
}
\lstset{columns=fullflexible}
\begin{lstlisting}
# Start by creating a random sample with 10 numbers:

set.seed(123)      

# this will allow everyone to work with the same random 
# sample. To change it, substitute the number in the 
# parenthesis. For this exercise, keep this number.

ex6 <- sample(x = 0:100, size = 10, replace = TRUE) 

# The sample function generates a sample of size 10 
# using numbers from 0 to 100, with replacement. 
\end{lstlisting}
\end{shaded}

结果:

在此处输入图片描述

然后,我尝试使用以下代码在幻灯片中复制它,但矩形仍然是黑色的。我尝试将包移动到不同的位置,但似乎没有任何东西能给我相同的结果。

代码:

\documentclass[10pt]{beamer}

\usetheme[progressbar=frametitle]{metropolis}

\usepackage{appendixnumberbeamer}
\usepackage{ocgx2} %implements PDF Layers
\usepackage{hyperref}
\usepackage[export]{adjustbox}
\usepackage{booktabs}
\usepackage[scale21]{ccicons}
\usepackage{graphicx}
\usepackage{array}
\usepackage{adjustbox}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usepackage{xspace}
\usepackage{fancyhdr}
\usepackage{multicol}
\usepackage{listings}
\usepackage[svgnames]{xcolor}
\usepackage{hyperref}
\usepackage{framed}
\definecolor{shadecolor}{named}{Gainsboro}
\usepackage{fancyhdr}

\subsection{Histogram}
\begin{frame}[fragile]{Histogram example}
Using R: 
\begin{shaded}
\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},
}
\lstset{columns=fullflexible}
\begin{lstlisting}
# From the integers 0:100, draw 20 numbers
set.seed(3640)
Sample <- sample(x = 0:100, size = 25, replace = TRUE)
df <- c(Sample)
df
\end{lstlisting}
\end{shaded}
\end{frame}

结果:

在此处输入图片描述

有什么办法可以让它变成灰色而不是黑色吗?

答案1

您不能将以下内容添加到您的 lstsettings 中吗?

backgroundcolor=\color{lightgray}

并且还摆脱了\begin{shaded}\end{shaded}为我做了这个技巧:

在此处输入图片描述

答案2

Beamer 会自动加载该xcolor包。您无法使用冲突的选项再次加载它。

相反,你可以用以下类选项告诉 beamer 你想要哪些选项:\documentclass[xcolor={svgnames}]{beamer}


其他一些评论:

  • 不要多次加载软件包。您可能会遇到导致背景颜色错误的选项冲突

  • 您不需要加载hyperrefgraphicx包,Beamer 会为您加载它们

  • 不要fancyhdr与 beamer 一起使用,beamer 有自己的头线和尾线机制

  • multicol我宁愿使用 beamer 自己的柱机制,而不是使用


\documentclass[10pt,xcolor=svgnames]{beamer}

\usetheme[progressbar=frametitle]{moloch}% modern fork of the metropolis theme

\usepackage{appendixnumberbeamer}
\usepackage{ocgx2} %implements PDF Layers
%\usepackage{hyperref}
\usepackage[export]{adjustbox}
\usepackage{booktabs}
\usepackage{ccicons}
%\usepackage{graphicx}
\usepackage{array}
%\usepackage{adjustbox} 
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usepackage{xspace}
%\usepackage{fancyhdr}
%\usepackage{multicol}
\usepackage{listings}
%\usepackage[]{xcolor}
%\usepackage{hyperref}
\usepackage{framed}
\definecolor{shadecolor}{named}{Gainsboro}
%\usepackage{fancyhdr}

\begin{document}

\subsection{Histogram}
\begin{frame}[fragile]{Histogram example}
Using R: 
\begin{shaded}
\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},
}
\lstset{columns=fullflexible}
\begin{lstlisting}
# From the integers 0:100, draw 20 numbers
set.seed(3640)
Sample <- sample(x = 0:100, size = 25, replace = TRUE)
df <- c(Sample)
df
\end{lstlisting}
\end{shaded}
\end{frame}

\end{document}

在此处输入图片描述

相关内容