使用 tcolorbox 帮助编写教科书练习的变体

使用 tcolorbox 帮助编写教科书练习的变体

我想在教科书项目中使用 tcolorbox 制作练习的基本工具,因此我转换了选项lowerbox=ignored中的选项lowerbox=invisible以获得与学生一起写解决方案的空白处,并删除了命令after upper={...Solution on page...

更确切地说,我添加了\NewTColorBox[auto counter,number within=chapter]{exercise}选项

skin=bicolor,
lowerbox=invisible,
colbacklower=white,

我删除了选项

after upper={\par\hfill\textcolor{green!40!black}%
    {\itshape Solution on page~\pageref{solution:#1}}},

现在我想为我的解决方案框添加一个标题,因此请在解决方案框的开头插入一个标题(在我的示例中,我希望标题以白色区域开头)。我该怎么做?

一个次要的(也是重要的)问题:我尝试在“真实条件”下使用该选项skin=bicolor,lowerbox=invisible,colbacklower=white,,即在教科书的所有练习中,我使用了几个包,但收到了错误消息:

`! Package tikz Error: Extra options not allowed for clipping path command..
 \pgfutil@packageerror ...ge {Package #1 Error: #2}`

这里有什么问题?我该如何解决?

这是我的 .tex 文件:

\documentclass{book}

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

\usepackage{polyglossia}
\setmainlanguage{english}

\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}}},
    skin=bicolor,
    lowerbox=invisible,
    colbacklower=white,
    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}
\pagestyle{empty}

\tcbstartrecording
\begin{exercise}{Ex}
An exercise.
\tcblower
A solution
\end{exercise}

\tcbstoprecording


\end{document}

并使用 xelatex 进行编译:

在此处输入图片描述

答案1

这会在行下方添加一个节点segmentation并将其移到内部,以便与左标题跳过对齐。

\kvtcb@X包含左上角(X=leftupper)和一般框分隔(X=boxsep)之间的相关长度

\documentclass{book}

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

\usepackage{polyglossia}
\setmainlanguage{english}

\makeatletter
\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}}},
    skin=bicolor,
    lowerbox=invisible,
    colbacklower=white,
    overlay={\node[right,inner sep=0pt,outer sep=0pt] (A) at ($(segmentation.south west)+(\kvtcb@leftupper+\kvtcb@boxsep,0)$) {Answer:};}
    savelowerto=solutions/exercise-\thetcbcounter.tex,
    record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
    #2
}
\makeatother

\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}
\pagestyle{empty}

\tcbstartrecording
\begin{exercise}{Ex}
An exercise.
\tcblower
A solution
\end{exercise}

\tcbstoprecording


\end{document}

在此处输入图片描述

相关内容