
Estou tentando reescrever um ambiente de barra lateral que tenho wrapfigure
e que é baseado em algo baseado em tcolorbox
. No entanto, encontrei alguns problemas.
Para referência, aqui está meu antigo ambiente:
\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
Funciona, mas está longe de ser bonito. É por isso que estou procurando usar tcolorbox
, já que tive bom sucesso ao usá-lo em outros contextos.
Aqui está um MWE do que tenho até agora:
% !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}
E aqui está como ele é renderizado:
Meus problemas são:
- Não sei como fazer com que as referências funcionem como funcionavam no meu ambiente anterior. Como você pode ver, meu ambiente antigo usa
renewcommand
ephantomsection
para atribuir o texto do rótulo para que as referências a uma barra lateral imprimam apenas o título da barra lateral (usandonameref
). Não tenho ideia de como conseguir o mesmo comnewtcolorbox
. - Da mesma forma, eu costumava
colorlet
substituir a cor da saída do código embutido pelocode
comando. Suspeito que seja uma variação da pergunta 1, mas não tenho certeza de ondecolocaressa lógica agora que estou usandonewtcolorbox
.
Alguém pode ajudar?
Responder1
Para o problema com referência errada nameref
utilize a \nameref
chave, que pode ser configurada para o título.
Como não há contador envolvido, label=
não há controle para fazer nada útil. Forneça auto counter
como opção de inicialização da nova definição de caixa.
\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}