我想将原来的Listing 1.1: Caption
风格改为1.1. listing.
Caption
。
我已经在以下线程中找到了解决此问题的两种可能的解决方案: 如何反转列表中的标题编号。不幸的是,它们都不适合我。我怀疑这是因为我使用列表的方式非常特殊。
这是我的配置(我从许多 TexStackExchange 帖子中创建),其中包含一个最少的工作示例。
\documentclass[12pt,a4paper,oneside]{report}
\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox} % https://tex.stackexchange.com/questions/305387/adding-a-caption-to-a-tcolorbox-tcblisting
\tcbuselibrary{listings,minted,skins,breakable}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}
\renewcommand{\lstlistingname}{Kódrészlet}
\renewcommand{\listoflistingscaption}{Kódrészletek jegyzéke}
\makeatletter
\renewcommand\fnum@lstlisting{%
\ifx\lst@@caption\@empty\else\thelstlisting~\fi%
\lstlistingname}%
\makeatother
\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother
\AtBeginDocument{%
\newtcblisting[blend into=listings]{code}[3]{%
breakable,
colback=codebg,
colframe=black!40,
enhanced,
listing engine=minted,
listing only,
left=5mm,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
listing remove caption=false,
minted style=colorful,
minted language=#1,
minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
coltitle=black,
attach boxed title to bottom center={yshift=-10pt},
boxed title style={enhanced jigsaw, colback=white, sharp corners, boxrule=0pt},
#2
}
}
\definecolor{codebg}{rgb}{0.95,0.95,0.95}
\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}
\begin{document}
\tableofcontents\vfill
\newpage
\begin{otherlanguage}{english}
\begin{code}{swift}{title={Test title}, label={lst:test-code}}
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
\end{code}
\end{otherlanguage}
\listAref{test-code} egy tesztkódot mutat.
\listoflistings\addcontentsline{toc}{chapter}{Kódrészletek jegyzéke}
\end{document}
一切正常,但fnum@lstlisting
似乎被忽略了(?)。我做错了什么?
答案1
我做了两处改变来解决这个问题。
首先,blend into=listings
构建一个替代方案
\tcbset{
new/blend into/revlistings/.style={use counter*=lstlisting,list inside=lol,/tcb/code={\appto\tcb@new@colopt{,before title={\tcb@blend@beforetitle{\thetcbcounter~\lstlistingname}}}}},%
}
新的blend into=revlistings
用名称反转数字。
第二,
blend before title code={##1.\ },
在number+name文字后添加了用于交换的:
by 。.
完整代码如下:
\documentclass[12pt,a4paper,oneside]{report}
\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox} % http://tex.stackexchange.com/questions/305387/adding-a-caption-to-a-tcolorbox-tcblisting
\tcbuselibrary{listings,minted,skins,breakable}
\renewcommand*\thelstnumber{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}
\renewcommand{\lstlistingname}{Kódrészlet}
\renewcommand{\listoflistingscaption}{Kódrészletek jegyzéke}
\makeatletter
\renewcommand\fnum@lstlisting{%
\ifx\lst@@caption\@empty\else\thelstlisting~\fi%
\lstlistingname}%
\tcbset{
new/blend into/revlistings/.style={use counter*=lstlisting,list inside=lol,/tcb/code={\appto\tcb@new@colopt{,before title={\tcb@blend@beforetitle{\thetcbcounter~\lstlistingname}}}}},%
}
\makeatother
\makeatletter
\AtBeginDocument{\let\c@listing\c@lstlisting}
\makeatother
\AtBeginDocument{%
\newtcblisting[blend into=revlistings]{code}[2]{%
blend before title code={##1.\ },
breakable,
colback=codebg,
colframe=black!40,
enhanced,
listing engine=minted,
listing only,
left=5mm,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
listing remove caption=false,
minted style=colorful,
minted language=#1,
minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
coltitle=black,
attach boxed title to bottom center={yshift=-10pt},
boxed title style={enhanced jigsaw, colback=white, sharp corners, boxrule=0pt},
#2
}
}
\definecolor{codebg}{rgb}{0.95,0.95,0.95}
\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}
\begin{document}
\tableofcontents\vfill
\newpage
\begin{otherlanguage}{english}
\begin{code}{swift}{title={Test title}, label={lst:test-code}}
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
\end{code}
\end{otherlanguage}
\listAref{test-code} egy tesztkódot mutat.
\listoflistings\addcontentsline{toc}{chapter}{Kódrészletek jegyzéke}
\end{document}
答案2
这个答案不能完全解决问题,但我希望它有所帮助。
除了从库中选择,我在文档中找不到tcolorbox
任何其他用于反转框号和名称的选项。但它不起作用,我不知道如何将 转换为。 theorem number and name
theorems
tcblistings
tcbtheorem
tcblisting
因此,我建议使用tcblisting
来格式化代码,但不要将其用作浮点数,而是创建一个新的浮点数 ( \usepackage{newfloat}
),用于包含非浮点数tcblisting
。在这种情况下,标题将对应于新的浮点数类型,您可以使用以下方法将其反转托尔斯滕·多尼格相关问题的解决方案。
开始使用的一些代码:
\documentclass[12pt,a4paper,oneside]{report}
\usepackage{t1enc}
\usepackage[english,magyar]{babel}
\usepackage[utf8]{inputenc}
\usepackage[chapter]{minted}
\usepackage{tcolorbox}
\usepackage{newfloat}
\DeclareFloatingEnvironment[%
listname={List of My Listings},
name=MyListing,
fileext=loml]{mylisting}
\usepackage{caption}
\DeclareCaptionLabelFormat{reverse}{#2 #1}
\captionsetup{labelformat=reverse}
\tcbuselibrary{listings,minted,skins,breakable}
\newtcblisting{code}[2][]{%
breakable,
colback=codebg,
colframe=black!40,
enhanced,
listing engine=minted,
listing only,
left=5mm,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
minted style=colorful,
minted language=#2,
minted options={linenos=true,numbersep=3mm,texcl=true,breaklines=true,autogobble=true},
coltitle=black,
#1
}
\definecolor{codebg}{rgb}{0.95,0.95,0.95}
%\newcommand{\listAref}[1]{\Aref{lst:#1}. kódrészlet}
\begin{document}
\begin{mylisting}
\begin{otherlanguage}{english}
\begin{tcblisting}{
enhanced,
breakable,
colback=codebg,
colframe=black!40,
listing engine=minted,
listing only,
left=5mm,
minted style=colorful,
minted language=swift,
minted options={linenos=true, numbersep=3mm, texcl=true,
breaklines=true, autogobble=true},
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
}
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
\end{tcblisting}
\end{otherlanguage}
\caption{First}
\end{mylisting}
\end{document}