1

I'm trying to re-write a sidebar environment I have that is based on wrapfigure and such into something based on tcolorbox. However, I've run into a couple of issues.

For reference, here is my old environment:

\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

It works, but it's far from pretty. That's why I'm looking to use tcolorbox, since I've had good success using it in other contexts.

Here's a MWE of what I have so far:

% !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}

And here's how it renders:

enter image description here

My problems are:

  1. I don't know how to get references to work like they did in my prior environment. As you can see, my old environment uses renewcommand and phantomsection to assign the label text so that references to a sidebar would just print out the sidebar's title (using nameref). I have no idea how to achieve the same with newtcolorbox.
  2. Similarly, I previously used colorlet to override the color for inline code output by the code command. I suspect this is a variation on question 1, but I'm not sure where to put this logic now that I'm using newtcolorbox.

Can anyone help?

1
  • I would help in order to know what referencing output you would expect from this new tcolorbox environment. There's no code showing what sidebar was supposed to do
    – user31729
    Feb 23, 2018 at 10:32

1 Answer 1

2

For the issue with the wrong nameref reference use the \nameref key, which can be set to the title.

Since there is no counter involved, label= has no grip to do anything useful. Provide auto counter as init option of the new box definition.

\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}
1
  • 1
    Thanks! For question 2 I put the \colorlet in the title and it seemed to work fine (even resets it automatically instead of making me manage that). The final problem (I think) I had is that the code command had an explicit font size, which was too big for the sidebar. I fixed that by using relsize instead. Feb 24, 2018 at 0:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .