我如何才能修复标题宽度以匹配列表?如下图所示:
代码在这里:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[a4paper,margin=1cm,footskip=.5cm]{geometry}
\usepackage{inconsolata}
\definecolor{eclipseBlue}{RGB}{42,0.0,255}
\definecolor{eclipseGreen}{RGB}{0,100,0}
\definecolor{eclipsePurple}{RGB}{127,0,85}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset
{
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{black},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
basicstyle=\small\ttfamily, % Global Code Style
keywordstyle=\color{blue},
identifierstyle=\color{black},
commentstyle=\color{eclipseGreen},
numbers=left, % show line numbers at the left
numberstyle=\small\ttfamily, % style of the line numbers
backgroundcolor=\color{black!7},
tabsize=2,
columns=flexible,
}
\begin{document}
\begin{lstlisting}[title=Test]
Code
a
b
b
c
c
d
s
we
wds
s
dsd
sd
sd
sd
gasdg
d
g
\end{lstlisting}
\end{document}
答案1
您应该考虑\fboxsep
填充。为了统一,我建议还添加黑色边框。
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
%\usepackage[a4paper,margin=1cm,footskip=.5cm]{geometry}% commented for the example
\usepackage{inconsolata}
\usepackage{calc}
\definecolor{eclipseBlue}{RGB}{42,0.0,255}
\definecolor{eclipseGreen}{RGB}{0,100,0}
\definecolor{eclipsePurple}{RGB}{127,0,85}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{%
\fcolorbox{black}{gray}{\parbox{\textwidth-2\fboxsep-2\fboxrule}{#1#2#3}}%
}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\lstset{
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{black},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
basicstyle=\small\ttfamily, % Global Code Style
keywordstyle=\color{blue},
identifierstyle=\color{black},
commentstyle=\color{eclipseGreen},
numbers=left, % show line numbers at the left
numberstyle=\small\ttfamily, % style of the line numbers
backgroundcolor=\color{black!7},
tabsize=2,
columns=flexible,
}
\begin{document}
\begin{lstlisting}[title=Test]
Code
a
b
b
c
c
d
s
we
wds
s
dsd
sd
sd
sd
gasdg
d
g
\end{lstlisting}
\end{document}