サイドバーにtcolorboxを使用する

サイドバーにtcolorboxを使用する

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}

レンダリングすると次のようになります:

ここに画像の説明を入力してください

私の問題は次のとおりです:

  1. 以前の環境と同じように参照を動作させる方法がわかりません。ご覧のとおり、以前の環境では と を使用してrenewcommandラベルphantomsectionテキストを割り当てているため、サイドバーへの参照ではサイドバーのタイトル ( を使用nameref) が出力されるだけです。 で同じことを実現する方法がわかりませんnewtcolorbox
  2. 同様に、私は以前、colorletコマンドによるインラインコード出力の色を上書きしていましたcode。これは質問1のバリエーションだと思いますが、どこを置く私が今使っているのはこのロジックですnewtcolorbox

誰か助けてくれませんか?

答え1

nameref間違った参照の問題については\nameref、タイトルに設定できるキーを使用します。

カウンターが関係していないため、label=何か役に立つことを行うためのグリップがありません。auto counter新しいボックス定義の init オプションとして提供します。

\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}

関連情報