我正在尝试修改我的列表环境,使列表的背景颜色扩展到整个页面的宽度。这样就不留白边了。
这可能吗?
我尝试做了一些研究,但似乎无法找到解决方案。
这是我当前的列表环境:
\documentclass{article}
\usepackage{color}
\usepackage{listings}
\definecolor{MyLightGray}{RGB}{200, 200,200}
\lstdefinelanguage{turtle}
{
columns=fullflexible,
keywordstyle=\color{red},
morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
morecomment=[l]{\#},
tabsize=4,
alsoletter={-?}, % allowed in names
morecomment=[s][\color{blue}]{<}{>},
basicstyle=\ttfamily\color{black},
%numberstyle=\color{black},
morestring=[b][\color{black}]\",
backgroundcolor=\color{MyLightGray},
}
\begin{document}
\begin{lstlisting}[language=turtle, captionpos=t]
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{lstlisting}
\end{document}
这是输出:
我不希望代码显示在文本宽度之外,只希望列表的背景窗格展开。
任何帮助都值得赞赏
快速补充。我注意到我的 pdf 阅读器上的代码行之间有小白线。它们在打印时不显示,但这可能是由于浅灰色背景。这只是视觉错觉还是当我选择较暗的背景颜色时它们会更突出。
答案1
我的答案是使用该tcolorbox
包来帮助解决装箱问题。请注意,您应该使用该包的当前版本3.12 (2014/07/29)
,即您可能需要先更新。
版本 1:
解决方案的基本思路是将列表用tcolorbox
放大到页面边缘的框起来。在这里,我使用了 5cm 的粗暴放大,而没有对实际边距进行微调。但是,如果您的实际边距不大于 5cm,那么这也适用于双面文档。
\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}
\definecolor{MyLightGray}{RGB}{200, 200,200}
\lstdefinelanguage{turtle}
{
columns=fullflexible,
keywordstyle=\color{red},
morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
morecomment=[l]{\#},
tabsize=4,
alsoletter={-?}, % allowed in names
morecomment=[s][\color{blue}]{<}{>},
basicstyle=\ttfamily\color{black},
%numberstyle=\color{black},
morestring=[b][\color{black}]\",
backgroundcolor=\color{MyLightGray},
}
\NewTCBListing{widelisting}{ O{} O{} }{
listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
enhanced,sharp corners,size=tight,boxrule=0pt,frame hidden,
width=\textwidth+10cm,
enlarge left by=-5cm,left=5cm,
enlarge right by=-5cm,right=5cm,
colback=MyLightGray,
#2 }
\begin{document}
\begin{lstlisting}[language=turtle, captionpos=t]
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{lstlisting}
\begin{widelisting}[language=turtle]
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{widelisting}
\end{document}
您将发现,使用预览器查看时,新环境widelisting
也没有细小的白线。
版本 2:
如果您想要为列表添加标题或标题,则必须进行一些修改。为了模仿原始的lstlisting
,可以使用以下内容,它与普通的列表:
\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}
\definecolor{MyLightGray}{RGB}{200, 200,200}
\lstdefinelanguage{turtle}
{
columns=fullflexible,
keywordstyle=\color{red},
morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
morecomment=[l]{\#},
tabsize=4,
alsoletter={-?}, % allowed in names
morecomment=[s][\color{blue}]{<}{>},
basicstyle=\ttfamily\color{black},
%numberstyle=\color{black},
morestring=[b][\color{black}]\",
backgroundcolor=\color{MyLightGray},
}
\NewTCBListing[use counter=lstlisting,list inside=lol,list type=lstlisting]{widelistingtitled}{ O{} O{} m }{
listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
enhanced,sharp corners,size=tight,boxrule=0pt,frame hidden,
width=\textwidth+10cm,
enlarge left by=-5cm,left=5cm,
enlarge right by=-5cm,right=5cm,
colback=MyLightGray,
title=Listing~\thetcbcounter:~#3,
list entry={\protect\numberline{\thetcbcounter}#3},
attach boxed title to top center,coltitle=black,
boxed title style={width=textwidth,empty},
#2 }
\begin{document}
\lstlistoflistings
\begin{lstlisting}[language=turtle, captionpos=t, caption=Original Example]
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{lstlisting}
\begin{widelistingtitled}[language=turtle]{Wide Listing with Title}
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{widelistingtitled}
\end{document}
输出为:
版本 3:
作为一种变体,我在这里使用了一些颜色和附加线条:
\documentclass{article}
\usepackage{color}
\usepackage[listings,skins,breakable,xparse]{tcolorbox}
\definecolor{MyLightGray}{RGB}{200, 200,200}
\lstdefinelanguage{turtle}
{
columns=fullflexible,
keywordstyle=\color{red},
morekeywords={@prefix,@base,@forSome,@forAll,@keywords},
morecomment=[l]{\#},
tabsize=4,
alsoletter={-?}, % allowed in names
morecomment=[s][\color{blue}]{<}{>},
basicstyle=\ttfamily\color{black},
%numberstyle=\color{black},
morestring=[b][\color{black}]\",
backgroundcolor=\color{MyLightGray},
}
\NewTCBListing[use counter=lstlisting,list inside=lol,list type=lstlisting]{widelistingtitled}{ O{} O{} m }{
listing only,breakable,listing options={#1,aboveskip=0pt,belowskip=0pt},
enhanced,sharp corners,size=tight,top=1mm,bottom=1mm,boxrule=0.5mm,
colframe=MyLightGray!50!blue,
width=\textwidth+10cm,
enlarge left by=-5cm,left=5cm,
enlarge right by=-5cm,right=5cm,
colback=MyLightGray,
title=Listing~\thetcbcounter:~#3,
list entry={\protect\numberline{\thetcbcounter}#3},
attach boxed title to top center={yshift=-0.4mm},coltitle=black,
boxed title style={width=textwidth,colback=blue!15!white,sharp corners=south},
#2 }
\begin{document}
\lstlistoflistings
\begin{lstlisting}[language=turtle, captionpos=t, caption=Original Example]
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{lstlisting}
\begin{widelistingtitled}[language=turtle]{Wide Listing with Title}
@prefix something: <http://example.org/something/>
something:subject something:predicate something:object .
\end{widelistingtitled}
\end{document}
得出: