本のリスト例があります。
キャプションとリストの間に、tcolorbox の幅と同じ水平線を付けたいと思います。
これを作成するために使用されたコード:
\documentclass[letterpaper,oneside,12pt]{book}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{caption}
\usepackage{xcolor}
\definecolor{shadecolor}{gray}{0.95}
\definecolor{captionbox}{cmyk}{0.43, 0.35, 0.35,0.01}
\tcbset{colback=captionbox!5!white,colframe=captionbox!75!black}
\BeforeBeginEnvironment{lstlisting}{\begin{tcolorbox}[toprule=3mm]\vskip-.5\baselineskip}
\AfterEndEnvironment{lstlisting}{\end{tcolorbox}}
\DeclareCaptionFormat{listing}{\parbox{\textwidth}{#1#2#3}}
\captionsetup[lstlisting]{format=listing,skip=10pt}
\lstset{numbers=none}
\begin{document}
\begin{lstlisting}[caption=Sample code block]
This is a code block
\end{lstlisting}
\end{document}
唯一の制約は、Pandoc がリストにそれを必要とするため、listings パッケージと lstlisting を使用する必要があることです。キャプションとリストの周囲にボックスを配置する効果と、キャプションとリストの間のルール、および同様のスタイルの感覚を生み出すために、他のものを喜んで置き換えます。
答え1
アップデート:行が連続したボックス内にも表示されるようにoverlay first
に変更しました。私の間違いです。overlay unbroken and first
残念ながら、 は使用できませんtcblistings
。 は、この目的のために特別に作成され、内部で を使用しているようですlistings
(私はこれを読みましたが、確認していません)。
私は「特定の」解決策(つまり、手動で調整する必要がある解決策)が好きではありませんが、それでも「機能する」という理由だけでこれを提案します。
\documentclass[letterpaper,oneside,12pt]{book}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{skins}
\usepackage{caption}
\usepackage{tikz}
\usepackage{lipsum}
\definecolor{shadecolor}{gray}{0.95}
\definecolor{captionbox}{cmyk}{0.43, 0.35, 0.35,0.01}
\tcbset{%
colback=captionbox!5!white,%
colframe=captionbox!75!black,%
top=1mm,% %% Used to manually align the caption with the horizontal line
%
%% Create a new "style" for your titled listings tcolorbox
mylistingwithtitle/.style = {%
breakable,%
%% Use tcolorbox's internal tikz object name (frame) to draw a horizontal line
overlay unbroken and first={\draw[shorten >=1.4pt, shorten <=1.4pt] ([yshift=-3em]frame.north west) -- ([yshift=-3em]frame.north east);}%
}%
}
\BeforeBeginEnvironment{lstlisting}{%
\begin{tcolorbox}[enhanced, toprule=3mm, mylistingwithtitle]%
\vskip-.5\baselineskip%
}
\AfterEndEnvironment{lstlisting}{\end{tcolorbox}}
\DeclareCaptionFormat{listing}{\parbox{\textwidth}{#1#2#3}}
\captionsetup[lstlisting]{format=listing,skip=15pt}
\begin{document}
%% This following line is only useful to execute \lipsum[1-4] inside the listing
\lstset{numbers=none, escapeinside={(*}{*)}}
\begin{lstlisting}[caption=Sample code block]
This is a code block
(*\lipsum[1-4]*)
\end{lstlisting}
\end{document}
キャプションの書式設定を変更する場合は、垂直方向の間隔、つまりtop=1mm
tcolorbox のオプションと水平線を描画するための を手動で調整する必要があります。また、tcolorbox の書式設定 (上部の線、境界線の幅、内側の余白など) を変更する場合は、後者のオプションとおよびオプション[yshift=-3em]
を調整する必要があります。shorten >=1.4pt
shorten <=1.4pt
breakable
tcolorbox にオプションを追加して、複数のページにまたがって表示できるようにしました。
出力: