
我正在嘗試將我所擁有的側邊欄環境重寫wrapfigure
為基於tcolorbox
.但是,我遇到了幾個問題。
作為參考,這是我的舊環境:
\makeatletter
\newenvironment*{sidebar}[3][0.5\textwidth]
{
% less vertical margin around wrapfigures
\setlength{\intextsep}{0pt}
\colorlet{savedcolor}{inline code}
\colorlet{inline code}{inline code inverted}
\renewcommand{\dummy}{#1}
\wrapfigure{#2}{#1}
\renewcommand{\@currentlabel}{#3}
\renewcommand{\@currentlabelname}{#3}
\phantomsection
\rule{#1}{1pt}
\rule{#1}{18pt}
\vspace{-18pt}
\centerline{\textcolor{white}{#3}}
\vspace{5pt}
\footnotesize
\leftskip=5pt
\rightskip=5pt
\setlength{\parskip}{0.2cm}
\setlength{\parindent}{0pt}
% restore the inline code color for the body of the bar
\colorlet{inline code}{savedcolor}
}
{
\leftskip=0pt
\rightskip=0pt
\setlength{\parindent}{0pt}
\rule{\dummy}{1pt}
\rule[.19in]{\dummy}{2.5pt}
\endwrapfigure
}
\makeatother
它確實有效,但遠談不上漂亮。這就是我希望使用 的原因tcolorbox
,因為我在其他情況下使用它取得了很好的成功。
這是我迄今為止所擁有的 MWE:
% !TEX program = xelatex
\documentclass{scrbook}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{hyperref}
\definecolor{inline code}{RGB}{194,61,53}
\definecolor{inline code inverted}{RGB}{193,153,151}
\newcommand*{\code}{\lstinline[basicstyle=\fontsize{9}{11}\ttfamily\color{inline code},keywordstyle=\color{inline code},stringstyle=\color{inline code},keepspaces=true]}
\newtcolorbox{cbar}[2][]{
parbox=false, % normal paragraph spacing
height from=2.5cm to 100cm,
halign=justify,
sharp corners,
colframe=black,
colback=black!15!white,
fontupper=\tiny,% font size for body of text
title=\scriptsize \textsc{#2},
#1
}
\begin{document}
\begin{cbar}[label=lab:testing]{Test Bar (some \code{Code})}
Here is a test bar with some \code{Code}.
\blindtext
\end{cbar}
Hello, \code{code}. And here is a reference to \ref{lab:testing}, which has name \nameref{lab:testing}.
\end{document}
它的渲染方式如下:
我的問題是:
- 我不知道如何像他們在我之前的環境中那樣獲得工作參考。正如您所看到的,我的舊環境使用
renewcommand
和phantomsection
來分配標籤文本,以便對側邊欄的引用只會列印出側邊欄的標題(使用nameref
)。我不知道如何實現同樣的目標newtcolorbox
。 - 同樣,我以前曾經使用過
colorlet
覆蓋命令輸出的內聯代碼的顏色code
。我懷疑這是問題 1 的變體,但我不知道在哪裡放現在我正在使用這個邏輯newtcolorbox
。
有人可以幫忙嗎?
答案1
對於引用錯誤的問題nameref
使用\nameref
key,可以將其設定為標題。
由於沒有涉及計數器,label=
因此無法執行任何有用的操作。提供auto counter
新框定義的初始化選項。
\documentclass{scrbook}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{hyperref}
\definecolor{inline code}{RGB}{194,61,53}
\definecolor{inline code inverted}{RGB}{193,153,151}
\newcommand*{\code}{\lstinline[basicstyle=\fontsize{9}{11}\ttfamily\color{inline code},keywordstyle=\color{inline code},stringstyle=\color{inline code},keepspaces=true]}
\newtcolorbox[auto counter]{cbar}[2][]{
parbox=false, % normal paragraph spacing
height from=2.5cm to 100cm,
halign=justify,
sharp corners,
colframe=black,
colback=black!15!white,
fontupper=\tiny,% font size for body of text
title={\thetcbcounter\ \scriptsize \textsc{#2}},
nameref={\scriptsize \textsc{#2}},
#1
}
\begin{document}
\begin{cbar}[label=lab:testing]{Test Bar (some \code{Code})}
Here is a test bar with some \code{Code}.
\blindtext
\end{cbar}
Hello, \code{code}. And here is a reference to \ref{lab:testing}, which has name \nameref{lab:testing}.
\end{document}