tcolorbox で作成したラベル付け演習

tcolorbox で作成したラベル付け演習

私は、tcolorbox の最新バージョンのドキュメントを使用して、ブック環境で章ごとに演習問題を作成しています。このバージョンでは、ラベルは自動的に作成されます (コマンド を使用label={exercise@\thetcbcounter})。以前のバージョンと同様に、コマンド と同じ方法で独自のラベルを作成したいと思います。label={exe:#2}, ここで#2、定義するラベルの名前を指定します。どうすればいいでしょうか?

ドキュメントにデータを含むファイルの例を示し、コマンドを使用します。

The solution of the exercise \ref{[email protected]} is in the page \pageref{[email protected]}

そこで、ラベル (.aux ファイル で見つかります!) と を、それぞれラベルと で呼び出したい[email protected]と思い[email protected]ますexercise:mylabelsolution:mylabel

\documentclass{book}


\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}

\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}


 \tcbuselibrary{skins,xparse}
\NewTColorBox[auto counter,number within=chapter]{exercise}{+O{}}{%
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=green!80!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~ \thetcbcounter:},
label={exercise@\thetcbcounter},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}},
#1
}
\NewTotalTColorBox{\solution}{mm}{%
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=red!50!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={solution@#1},
attach title to upper=\par,
}{\input{#2}}



\tcbset{no solution/.style={no recording,after upper=}}


\begin{document}
\chapter{The first chapter}

\tcbstartrecording
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f’(x) &= \left( \sin((\sin x)^2) \right)’
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\vspace*{1cm}
The solution of the exercise \ref{[email protected]} is in the page \pageref{[email protected]}
\vspace*{1cm}
\tcbstoprecording
\tcbinputrecords

\end{document}

XeLatex でコンパイルすると次のようになります。 ここに画像の説明を入力してください

答え1

次のコードは、 に必須の引数を追加します。この必須の引数は、 およびexerciseに追加されるラベルです。exercise:...solution:...

このラベルの導入により、tcolorboxのlabelafter upperおよびrecordオプション定義が変更されました。また、以前のは に変更されました。exercise#1#2

ボックスにはsolution新しい引数はありませんが、titleおよびに変更がありますphantomlabel

\documentclass{book}

\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}

\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}

\tcbuselibrary{skins,xparse}

\NewTColorBox[auto counter,number within=chapter]{exercise}{m+O{}}{%
    enhanced,
    colframe=green!20!black,
    colback=yellow!10!white,
    coltitle=green!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
        \shade[inner color=green!80!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
        \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
        \end{tcbclipinterior}},
    title={Exercise~ \thetcbcounter:},
    label={exercise:#1},
    attach title to upper=\quad,
    after upper={\par\hfill\textcolor{green!40!black}%
        {\itshape Solution on page~\pageref{solution:#1}}},
    lowerbox=ignored,
    savelowerto=solutions/exercise-\thetcbcounter.tex,
    record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
    #2
}

\NewTotalTColorBox{\solution}{mm}{%
    enhanced,
    colframe=red!20!black,
    colback=yellow!10!white,
    coltitle=red!40!black,
    fonttitle=\bfseries,
    underlay={\begin{tcbclipinterior}
        \shade[inner color=red!50!yellow,outer color=yellow!10!white]
            (interior.north west) circle (2cm);
        \draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
            (interior.south west) grid (interior.north east);
        \end{tcbclipinterior}},
    title={Solution of Exercise~\ref{exercise:#1} on page~\pageref{exercise:#1}:},
    phantomlabel={solution:#1},
    attach title to upper=\par,
}{\input{#2}}

\tcbset{no solution/.style={no recording,after upper=}}

\begin{document}
\chapter{The first chapter}

\tcbstartrecording
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f’(x) &= \left( \sin((\sin x)^2) \right)’
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\vspace*{1cm}
The solution of the exercise \ref{exercise:Ex1} is in the page \pageref{solution:Ex1}
\vspace*{1cm}
\tcbstoprecording
\tcbinputrecords

\end{document}

結果ファイルと補助ファイルは変更されず、ファイルの内容のみにaux新しいラベル構文と値が表示されます。

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

関連情報