我怎样才能制作一个易碎的盒子

我怎样才能制作一个易碎的盒子

我想自定义此代码

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{topbox}[2][]{
    title=#2,
    enhanced, breakable, 
    enlarge top initially by=1cm,
%    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and first={
        \draw[green!70!black, line width=.5mm, rounded corners]
            ([xshift=-5mm]frame.south west)|-([yshift=1cm]frame.north)-|
            ([xshift=5mm]frame.south east);
        \node[fill=green!70!black, minimum height=5mm, 
            minimum width=\linewidth, anchor=north] at ([yshift=1cm]frame.north) 
            (outertitle) {};
        \node[text=white, anchor=west] at ([xshift=3mm]outertitle.west) {Outer title};
        },
    overlay middle and last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\newtcolorbox{middlebox}[2][]{
    title=#2,
    enhanced, breakable, 
%    enlarge top initially by=1cm,
%    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([xshift=-5mm]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    overlay last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([xshift=5mm]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
    },
    #1
}

\newtcolorbox{bottombox}[2][]{
    title=#2,
    enhanced, breakable, 
%    enlarge top initially by=1cm,
    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and last={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)|-([yshift=-5mm]frame.south)
             -|([xshift=5mm]frame.north east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\begin{document}
\lipsum[1]
\begin{topbox}{this is the title}
\lipsum[1-2]
\end{topbox}
\begin{middlebox}[colframe=red, colback=pink!10]{this is the title}
\lipsum[1-5]
\end{middlebox}
\begin{middlebox}[colframe=brown!70!black, colback=brown!10]{this is the title}
\lipsum[1-2]
\end{middlebox}
\begin{bottombox}[colframe=blue, colback=cyan!10]{this is the title}
\lipsum[1-3]
\end{bottombox}
\lipsum[1]
\end{document}

通过引入此代码来更改内部框

\NewTColorBox[auto counter,number within=chapter]{exercise}{m+O{}}{%
enhanced jigsaw,enforce breakable,pad at break=0mm,
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=}}

我尝试过多次将我的代码集成到建议的代码中(顶部中间和底部框),但无法成功,总是出现错误。如果有人能帮助我,我将不胜感激。

答案1

你可以从这里开始:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{lmodern}

\tcbset{
    exercisestyle/.style={%
        enhanced,
        breakable,
        colframe=green!20!black,
        colback=yellow!10!white,
        coltitle=green!40!black,
        fonttitle=\bfseries,
        underlay unbroken and first={%
            \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}},
        underlay middle and last={%
            \begin{tcbclipinterior}
                \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}},       
    },
    no solution/.style={no recording, after upper=}
}

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

\NewTColorBox[auto counter, number within=section]{topexercise}{+!O{}}{%
    exercisestyle, 
    enlarge top initially by=1cm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and first={
        \draw[green!70!black, line width=.5mm, rounded corners]
            ([xshift=-5mm]frame.south west)|-([yshift=1cm]frame.north)-|
            ([xshift=5mm]frame.south east);
        \node[fill=green!70!black, minimum height=5mm, 
            minimum width=\linewidth, anchor=north] at ([yshift=1cm]frame.north) 
            (outertitle) {};
        \node[text=white, anchor=west, font=\bfseries] at ([xshift=3mm]outertitle.west) {Some exercises to solve \dots};
        },
    overlay middle and last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\NewTColorBox[use counter from=topexercise]{midexercise}{+!O{}}{%
    exercisestyle,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([xshift=-5mm]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    overlay last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([xshift=5mm]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
    },
    #1
}

\NewTColorBox[use counter from=topexercise]{botexercise}{+!O{}}{%
    exercisestyle,
    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and last={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)|-([yshift=-5mm]frame.south)
             -|([xshift=5mm]frame.north east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\begin{document}

\tcbstartrecording

\section{First section}

\lipsum[1]
\begin{topexercise}
\lipsum[1-2]
\tcblower
\lipsum[3]
\end{topexercise}

\begin{midexercise}
\lipsum[3-5]
\tcblower
\lipsum[6]
\end{midexercise}

\begin{botexercise}
\lipsum[6-9]
\tcblower
\lipsum[10]
\end{botexercise}
\tcbstoprecording

\section{Solutions}

\tcbinputrecords
\end{document}

在此处输入图片描述

更新:盒装解决方案

解决方案也可以像练习一样用方框包装,但在这种情况下无法使用\tcbinputrecords命令,每个解决方案都必须手动加载。这样,第一个解决方案将是topsolution,中间的将是midsolution,最后一个将是botsolution

top如果、mid以及bot练习和解决方案的样式定义像exercisestylesolutionstyle并且它们之间共享,则以下代码可以缩短。

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{lmodern}

\tcbset{
    exercisestyle/.style={%
        enhanced,
        breakable,
        colframe=green!20!black,
        colback=yellow!10!white,
        coltitle=green!40!black,
        fonttitle=\bfseries,
        underlay unbroken and first={%
            \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}},
        underlay middle and last={%
            \begin{tcbclipinterior}
                \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}},       
    },
    solutionstyle/.style={
        enhanced,
        breakable,
        colframe=red!20!black,
        colback=yellow!10!white,
        coltitle=red!40!black,
        fonttitle=\bfseries,
        underlay unbroken and first={%
            \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}},
        underlay middle and last={%
            \begin{tcbclipinterior}
                \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,     
    },
    no solution/.style={no recording, after upper=}
}

\NewTotalTColorBox{\topsolution}{mm}{%
     solutionstyle=#1,
    enlarge top initially by=1cm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and first={
        \draw[green!70!black, line width=.5mm, rounded corners]
            ([xshift=-5mm]frame.south west)|-([yshift=1cm]frame.north)-|
            ([xshift=5mm]frame.south east);
        \node[fill=green!70!black, minimum height=5mm, 
            minimum width=\linewidth, anchor=north] at ([yshift=1cm]frame.north) 
            (outertitle) {};
        \node[text=white, anchor=west, font=\bfseries] at ([xshift=3mm]outertitle.west) {Solutions to previous exercises};
        },
    overlay middle and last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
}{\input{#2}}

\NewTotalTColorBox{\midsolution}{mm}{%
    solutionstyle=#1,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([xshift=-5mm]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    overlay last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([xshift=5mm]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
    },
}{\input{#2}}

\NewTotalTColorBox{\botsolution}{mm}{%
    solutionstyle=#1,
    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and last={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)|-([yshift=-5mm]frame.south)
             -|([xshift=5mm]frame.north east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
}{\input{#2}}

\NewTColorBox[auto counter, number within=section]{topexercise}{+!O{}}{%
    exercisestyle, 
    enlarge top initially by=1cm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and first={
        \draw[green!70!black, line width=.5mm, rounded corners]
            ([xshift=-5mm]frame.south west)|-([yshift=1cm]frame.north)-|
            ([xshift=5mm]frame.south east);
        \node[fill=green!70!black, minimum height=5mm, 
            minimum width=\linewidth, anchor=north] at ([yshift=1cm]frame.north) 
            (outertitle) {};
        \node[text=white, anchor=west, font=\bfseries] at ([xshift=3mm]outertitle.west) {Some exercises to solve \dots};
        },
    overlay middle and last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\NewTColorBox[use counter from=topexercise]{midexercise}{+!O{}}{%
    exercisestyle,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([shift={(-5mm,5mm)}]frame.north west)--([xshift=-5mm]frame.south west)
             ([shift={(5mm,5mm)}]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    overlay last={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([shift={(-5mm,-5mm)}]frame.south west)
             ([xshift=5mm]frame.north east)--([shift={(5mm,-5mm)}]frame.south east);
    },
    #1
}

\NewTColorBox[use counter from=topexercise]{botexercise}{+!O{}}{%
    exercisestyle,
    enlarge bottom finally by=5mm,
    enlarge left by=5mm,
    enlarge right by=5mm,
    width=\linewidth-10mm,
    overlay unbroken and last={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)|-([yshift=-5mm]frame.south)
             -|([xshift=5mm]frame.north east);
        },
    overlay first={
        \draw[green!70!black, line width=.5mm]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
        },
    overlay middle={
        \draw[green!70!black, line width=.5mm, rounded corners]
             ([xshift=-5mm]frame.north west)--([xshift=-5mm]frame.south west)
             ([xshift=5mm]frame.north east)--([xshift=5mm]frame.south east);
    },
    #1
}

\begin{document}

\tcbstartrecording

\section{First section}

\lipsum[1]
\begin{topexercise}
\lipsum[1-2]
\tcblower
\lipsum[3]
\end{topexercise}

\begin{midexercise}
\lipsum[3-5]
\tcblower
\lipsum[6]
\end{midexercise}

\begin{botexercise}
\lipsum[6-9]
\tcblower
\lipsum[10]
\end{botexercise}
\tcbstoprecording

\section{Solutions}

%\tcbinputrecords
\topsolution{1.1}{solutions/exercise-1.1.tex}
\midsolution{1.2}{solutions/exercise-1.2.tex}
\botsolution{1.3}{solutions/exercise-1.3.tex}
\end{document}

在此处输入图片描述

相关内容