我正在尝试使用tcolorbox
并将所有 lstnumbers 设为两位数。似乎 hyperref 存在错误。我在下面附上了一个工作示例。
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings,skins,breakable}
% define colors for code blocks
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{codebackcolour}{rgb}{0.95,0.95,0.92}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage{fontawesome5}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}
\hypersetup{
pdfauthor={},
pdftitle={},
pdfkeywords={},
pdfsubject={},
pdfcreator={},
pdflang={English},
breaklinks=true,
colorlinks=true,
linkcolor=link,
urlcolor=url,
citecolor=cite
}
\urlstyle{same}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
% define the code snippet style
\lstdefinestyle{code}{
numbers=left,
numberstyle=\ttfamily\tiny,
captionpos=t,
abovecaptionskip=0pt,
belowcaptionskip=\medskipamount,
xleftmargin=0.5em,
basicstyle=\fontfamily{AnonymousPro}\selectfont,
keywordstyle=\bfseries\color{pblue},
stringstyle=\bfseries\itshape\color{green!40!black},
commentstyle=\bfseries\itshape\color{black!60},
showspaces=false,
tabsize=2,
extendedchars=true,
morekeywords={*,...}, % if more keywords should be added to the set
alsoletter={()[].=},
showtabs=false,
breaklines=true,
showstringspaces=false,
keepspaces=true,
breakatwhitespace=false,
escapeinside={!>}{<!},
tabsize=1,
emphstyle={\bfseries\color{pblue}},
columns=fullflexible,
literate={0}{0}{1}%
{1}{1}{1}%
{2}{2}{1}%
{3}{3}{1}%
{4}{4}{1}%
{5}{5}{1}%
{6}{6}{1}%
{7}{7}{1}%
{8}{8}{1}%
{9}{9}{1}%
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\EUR}}1 {£}{{\pounds}}1,
}
\newtcblisting[auto counter]{code}[1][]{
enhanced,
top=\smallskipamount,
bottom=2.5\smallskipamount,
sharp corners=downhill,
arc=0.5cm,
colframe=gray!20,
attach boxed title to top left={
xshift=-2mm
},
boxed title style={
size=small,
colback=white,
colframe=white,
boxrule=0pt,
top=0pt,
bottom=0pt,
right=0pt,
left=0pt
},
fonttitle=\color{black},
listing only,
listing options={
style=code
},
/codeFormattingKeys/.cd,#1,/tcb/.cd
}
\pgfkeys{
/codeFormattingKeys/title/.style = {/tcb/title={\ifx #1\empty\empty\else\ \textbf{Code Snippet~\thetcbcounter.} #1\fi}},
/codeFormattingKeys/language/.style = {/tcb/listing options app={language=#1}},
/codeFormattingKeys/file/.style = {
/tcb/overlay={
\begin{tcbclipinterior}
\fill[gray!25](frame.south west)
rectangle ([xshift=5.0mm]frame.north west);
\end{tcbclipinterior}
\node[inner sep=0.75mm,rounded corners=0.5mm,anchor=north east,fill=gray!50] at ([xshift=-2.5mm, yshift=\baselineskip + 1mm]frame.south east){\scriptsize\color{violet}\itshape\ttfamily#1};
},
}
}
% \renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}} % <--- problem
\definecolor{url}{HTML}{0077bb}
\definecolor{link}{HTML}{882255}
\definecolor{cite}{HTML}{999933}
\begin{document}
{
\hypersetup{hidelinks}
\tableofcontents
}
\begin{code}[language=Python, file={\faPython}]
print{"Hello World"}
\end{code}
\end{document}
我怎样才能解决这个问题?
答案1
您可以更改计数器的表示形式lstnumber
:
\renewcommand{\thelstnumber}{%
\ifnum\value{lstnumber}<10 0\fi\arabic{lstnumber}%
}
完整示例:
\documentclass{scrartcl}
%\usepackage[utf8]{inputenc}% <--- no longer necessary
\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings,skins,breakable}
% define colors for code blocks
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{codebackcolour}{rgb}{0.95,0.95,0.92}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage{fontawesome5}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}
\hypersetup{
pdfauthor={},
pdftitle={},
pdfkeywords={},
pdfsubject={},
pdfcreator={},
pdflang={English},
breaklinks=true,
colorlinks=true,
linkcolor=link,
urlcolor=url,
citecolor=cite
}
\urlstyle{same}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
% define the code snippet style
\lstdefinestyle{code}{
numbers=left,
numberstyle=\ttfamily\tiny,
captionpos=t,
abovecaptionskip=0pt,
belowcaptionskip=\medskipamount,
xleftmargin=0.5em,
basicstyle=\fontfamily{AnonymousPro}\selectfont,
keywordstyle=\bfseries\color{pblue},
stringstyle=\bfseries\itshape\color{green!40!black},
commentstyle=\bfseries\itshape\color{black!60},
showspaces=false,
tabsize=2,
extendedchars=true,
morekeywords={*,...}, % if more keywords should be added to the set
alsoletter={()[].=},
showtabs=false,
breaklines=true,
showstringspaces=false,
keepspaces=true,
breakatwhitespace=false,
escapeinside={!>}{<!},
tabsize=1,
emphstyle={\bfseries\color{pblue}},
columns=fullflexible,
literate={0}{0}{1}%
{1}{1}{1}%
{2}{2}{1}%
{3}{3}{1}%
{4}{4}{1}%
{5}{5}{1}%
{6}{6}{1}%
{7}{7}{1}%
{8}{8}{1}%
{9}{9}{1}%
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{€}{{\EUR}}1 {£}{{\pounds}}1,
}
\newtcblisting[auto counter]{code}[1][]{
enhanced,
top=\smallskipamount,
bottom=2.5\smallskipamount,
sharp corners=downhill,
arc=0.5cm,
colframe=gray!20,
attach boxed title to top left={
xshift=-2mm
},
boxed title style={
size=small,
colback=white,
colframe=white,
boxrule=0pt,
top=0pt,
bottom=0pt,
right=0pt,
left=0pt
},
fonttitle=\color{black},
listing only,
listing options={
style=code
},
/codeFormattingKeys/.cd,#1,/tcb/.cd
}
\pgfkeys{
/codeFormattingKeys/title/.style = {/tcb/title={\ifx #1\empty\empty\else\ \textbf{Code Snippet~\thetcbcounter.} #1\fi}},
/codeFormattingKeys/language/.style = {/tcb/listing options app={language=#1}},
/codeFormattingKeys/file/.style = {
/tcb/overlay={
\begin{tcbclipinterior}
\fill[gray!25](frame.south west)
rectangle ([xshift=5.0mm]frame.north west);
\end{tcbclipinterior}
\node[inner sep=0.75mm,rounded corners=0.5mm,anchor=north east,fill=gray!50] at ([xshift=-2.5mm, yshift=\baselineskip + 1mm]frame.south east){\scriptsize\color{violet}\itshape\ttfamily#1};
},
}
}
% \renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}} % <--- problem
\definecolor{url}{HTML}{0077bb}
\definecolor{link}{HTML}{882255}
\definecolor{cite}{HTML}{999933}
\renewcommand{\thelstnumber}{%
\ifnum\value{lstnumber}<10 0\fi\arabic{lstnumber}%
}
\begin{document}
{
\hypersetup{hidelinks}
\tableofcontents
}
\section{Code}
\begin{code}[language=Python, file={\faPython}]
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
print{"Hello World"}
\end{code}
\end{document}