问题

问题

问题

我正在尝试排版tcolorbox文档)solutionbox有两个不同的部分:upper部分用于解决方案,lower部分用于解释。

我已经定义了一个单独的sectionbox(它是一个内联\tcbox{}环境),它由我的每个部分的部分标题使用solutionbox

我正在使用before upper*before lower*选项添加这些标题solutionbox

这很完美,但我需要自己手动调整文本对齐方式。有没有更好的方法,最好是让后续文本行正确对齐,并且像itemize和其他环境这样的环境不会溢出到左侧?

平均能量损失

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}
\usepackage{calc}

% Colour
\usepackage[dvipsnames, cmyk]{xcolor}

% Customising lists
\usepackage{enumitem}
\setitemize{noitemsep,topsep=0pt,parsep=4pt,partopsep=0pt,leftmargin=*}

% tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{%
    skins
}

%% Custom tcolorboxes
\NewTCBox
    {\sectionbox}
    { O{Violet} !O{} }  % 1: colour, 2: extra options
    {
        on line,
        arc=7pt,
        colback=#1!2.5!white,
        colframe=#1!90!black,
        colupper=#1,
        boxrule=1pt,
        boxsep=0pt,
        left=6.5pt,
        right=3pt,
        top=3pt,
        bottom=3pt,
        before upper={\rule[-2.5pt]{0pt}{10pt}},
        #2
    }

%%% Custom lengths   ---   This is the annoying finnicky part
\newlength{\widthofexplanation}
\settowidth{\widthofexplanation}{\widthof{\sectionbox{\textbf{Explanation:}}}}
\addtolength{\widthofexplanation}{5mm}

\newlength{\setbackwidthofexplanation}
\setlength{\setbackwidthofexplanation}{\widthofexplanation}
\addtolength{\setbackwidthofexplanation}{1mm}

\NewTColorBox
    {solutionbox}
    { O{Solution:} O{Violet} !O{} }  % 1: before title declaration, 2: colour, 3: extra options
    {
        enhanced,
        detach title,
        coltitle=#2,
        colframe=#2,
        colback=white,
        before upper={%
            \hspace{-\setbackwidthofexplanation}\makebox[\widthofexplanation][b]{\sectionbox{\textbf{#1}}}~\parindent15pt\noindent
        },
        before lower={%
            \hspace{-\setbackwidthofexplanation}\makebox[\widthofexplanation][b]{\sectionbox{\textbf{Explanation:}}}~\parindent15pt\noindent
        },
        left=\widthofexplanation,
        #3
    }

\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\begin{enumerate}
    \item   Example question (the first one)
            \begin{solutionbox}
                \( x = math \)
            \end{solutionbox}
    \item   Example question (the second one)
            \begin{solutionbox}
                Written solution instead of math.
                %
                \tcblower
                %
                Here's a nice explanation for it. Enough to at least flow over to the next line. \\
                Here's another line for good measure :)
            \end{solutionbox}
    \item   Example question
            \begin{solutionbox}
                \( x = \frac{ -b \pm \sqrt{ b^2 - 4ac } }{ 2a } \) \\
                \( y = x^3 \)
                %
                \tcblower
                %
                \begin{itemize}
                    \item[\textbf{Sol. for \( x \):}] Blah blah blah.
                    \item[\textbf{Sol. for \( y \):}] More blah blah blah.
                \end{itemize}
            \end{solutionbox}
    
\end{enumerate}

\end{document}

图片

上面的 MWE 代码输出的图像;这就是未固定的输出。

答案1

在下面的完整示例中,配置为在\sectionbox的上半部分和下半部分的第一个段落的开头插入。将考虑列表环境设置的solutionbox非默认设置。\parshape

片段

        before upper={%
          \AddToHookNext{para/begin}{%
            \llap{%
              \sectionbox{\textbf{#1}}%
              % \parshapeindent1 is the indent of first line
              \hskip\dimexpr\parshapeindent1+5pt\relax
            }%
          }%
        },
  • \AddToHookNext{<hook>}{<code>}仅附加<code>到 的下一个使用<hook>处。它记录在 LaTeX2e 手册之一 中texdoc lthooks-doc
  • Hookpara/begin在 LaTeX2e 手册之一中有详细记录texdoc ltpara-doc
  • eTeX 原语的\parshapeindent文档记录在e-TeX 手册texdoc etex)。

完整示例。

请注意,itemize被 替换description。 使用 中的变量标签宽度itemize可能不是一个好主意。

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm]{geometry}
\usepackage{calc}

% Colour
\usepackage[dvipsnames, cmyk]{xcolor}

% Customising lists
\usepackage{enumitem}
\setlist[itemize]{noitemsep,topsep=0pt,parsep=4pt,partopsep=0pt,leftmargin=*}
\setlist[description]{noitemsep,topsep=0pt,parsep=4pt,partopsep=0pt}

% tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{%
    skins
}

%% Custom tcolorboxes
\NewTCBox
    {\sectionbox}
    { O{Violet} !O{} }  % 1: colour, 2: extra options
    {
        on line,
        arc=7pt,
        colback=#1!2.5!white,
        colframe=#1!90!black,
        colupper=#1,
        boxrule=1pt,
        boxsep=0pt,
        left=6.5pt,
        right=3pt,
        top=3pt,
        bottom=3pt,
        before upper={\rule[-2.5pt]{0pt}{10pt}},
        #2
    }

%%% Custom lengths   ---   This is the annoying finnicky part
\newlength{\widthofexplanation}
\settowidth{\widthofexplanation}{\widthof{\sectionbox{\textbf{Explanation:}}}}
\addtolength{\widthofexplanation}{5mm}

\newlength{\setbackwidthofexplanation}
\setlength{\setbackwidthofexplanation}{\widthofexplanation}
\addtolength{\setbackwidthofexplanation}{1mm}

\NewTColorBox
    {solutionbox}
    { O{Solution:} O{Violet} !O{} }  % 1: before title declaration, 2: colour, 3: extra options
    {
        enhanced,
        detach title,
        coltitle=#2,
        colframe=#2,
        leftupper=\widthofexplanation,
        leftlower=\widthofexplanation,
        colback=white,
        before upper={%
          \AddToHookNext{para/begin}{%
            \llap{%
              \sectionbox{\textbf{#1}}%
              % \parshapeindent1 is the indent of first line
              \hskip\dimexpr\parshapeindent1+5pt\relax
            }%
          }%
        },
        before lower={%
          \AddToHookNext{para/begin}{%
            \llap{%
              \sectionbox{\textbf{Explanation:}}%
              \hskip\dimexpr\parshapeindent1+5pt\relax
            }%
          }%
        },
        #3
    }

\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\begin{enumerate}
    \item   Example question (the first one)
            \begin{solutionbox}
                \( x = math \)
            \end{solutionbox}
    \item   Example question (the second one)
            \begin{solutionbox}
                Written solution instead of math.
                %
                \tcblower
                %
                Here's a nice explanation for it. Enough to at least flow over to the next line. \\
                Here's another line for good measure :)
            \end{solutionbox}
    \item   Example question
            \begin{solutionbox}
                \( x = \frac{ -b \pm \sqrt{ b^2 - 4ac } }{ 2a } \) \\
                \( y = x^3 \)
                %
                \tcblower
                %
                \begin{description}
                    \item[\textbf{Sol. for \( x \):}] Blah blah blah.
                    \item[\textbf{Sol. for \( y \):}] More blah blah blah.
                \end{description}
            \end{solutionbox}
    \item   More test
            \begin{solutionbox}
                \begin{itemize}
                    \item foo
                    \item bar
                \end{itemize}
                %
                \tcblower
                %
                \begin{description}
                    \item[\textbf{Sol. for \( x \):}] Blah blah blah.
                    \item[\textbf{Sol. for \( y \):}] More blah blah blah.
                \end{description}
            \end{solutionbox}
    
\end{enumerate}

\end{document}

在此处输入图片描述

答案2

我不确定我是否理解了这个问题。

为了在文本旁边放置列表,我建议使用minipage

注意:在我看来,环境description更符合您的要求。我使用\setlist\setitemize已贬值。

\documentclass[12pt, a4paper]{article}
\usepackage{lipsum}%<<-- for the test
% Layout
\usepackage[margin=2cm]{geometry}
\usepackage{calc}

% Colour
\usepackage[dvipsnames, cmyk]{xcolor}

% Customising lists
\usepackage{enumitem}
%\setitemize{noitemsep,topsep=0pt,parsep=4pt,partopsep=0pt,leftmargin=*}
\setlist[description]{
noitemsep,
topsep=0pt,
parsep=4pt,
partopsep=0pt,
% leftmargin=*
}

% tcolorbox
\usepackage{tcolorbox}
\tcbuselibrary{%
skins
}

%% Custom tcolorboxes
\NewTCBox
{\sectionbox}
{ O{Violet} !O{} }  % 1: colour, 2: extra options
{
    on line,
    arc=7pt,
    colback=#1!2.5!white,
    colframe=#1!90!black,
    colupper=#1,
    boxrule=1pt,
    boxsep=0pt,
    left=6.5pt,
    right=3pt,
    top=3pt,
    bottom=3pt,
    before upper={\rule[-2.5pt]{0pt}{10pt}},
    #2
}

%%% Custom lengths   ---   This is the annoying finnicky part
\newlength{\widthofexplanation}
\settowidth{\widthofexplanation}{\widthof{\sectionbox{\textbf{Explanation:}}}}
\addtolength{\widthofexplanation}{5mm}

\newlength{\setbackwidthofexplanation}
\setlength{\setbackwidthofexplanation}{\widthofexplanation}
\addtolength{\setbackwidthofexplanation}{1mm}

\NewTColorBox
{solutionbox}
{ O{Solution:} O{Violet} !O{} }  % 1: before title declaration, 2: colour, 3: extra options
{
    enhanced,
    detach title,
    coltitle=#2,
    colframe=#2,
    colback=white,
    before upper={%
        \hspace{-\setbackwidthofexplanation}\makebox[\widthofexplanation][b]{\sectionbox{\textbf{#1}}}~\parindent15pt\noindent
    },
    before lower={%
        \hspace{-\setbackwidthofexplanation}\makebox[\widthofexplanation][b]{\sectionbox{\textbf{Explanation:}}}~\parindent15pt\noindent
    },
    left=\widthofexplanation,
    #3
}

\title{\TeX{}.SE MWE}
\author{BrightBulb123}

\begin{document}

\maketitle

\section{Introduction}

\begin{enumerate}
\item   Example question (the first one)
        \begin{solutionbox}
            \( x = math \)
        \end{solutionbox}
\item   Example question (the second one)
        \begin{solutionbox}
            Written solution instead of math.
            %
            \tcblower
            %
            Here's a nice explanation for it. Enough to at least flow over to the next line. \\
            Here's another line for good measure :)
        \end{solutionbox}
\item   Example question
        \begin{solutionbox}
            \( x = \frac{ -b \pm \sqrt{ b^2 - 4ac } }{ 2a } \) \\
            \( y = x^3 \)
            %
            \tcblower
            %
            \begin{minipage}[t]{\linewidth}
                \begin{description}
                    \item[Sol. for \( x \):] \lipsum[1]
                    \item[Sol. for \( y \):] More blah blah blah.
                    \end{description}
        \end{minipage}%
        \end{solutionbox}

\end{enumerate}

\end{document}

编辑:带有labelindent。我\DrawEnumitemLabel在 enumitem 文档中的第 3.2 节标签的水平间距中添加了。

\documentclass[12pt, a4paper]{article}

% Layout
\usepackage[margin=2cm,showframe]{geometry}

\usepackage{enumitem}
%\setitemize{noitemsep,topsep=0pt,parsep=4pt,partopsep=0pt,leftmargin=*}
\setlist[itemize]{
    noitemsep,
    topsep=0pt,
    parsep=4pt,
    partopsep=0pt,
    leftmargin=*,
    % labelindent=52pt,
    }
\usepackage{lipsum}
\newlength{\mylength}
\settowidth{\mylength}{Sol. for \( x \):}
\begin{document}
Without labelindent:
\begin{itemize}
    \DrawEnumitemLabel
    \item \lipsum[1]
    \item More blah blah blah.
\end{itemize}

\noindent\rule{\linewidth}{0.4pt}

With option in item

\begin{itemize}
    \DrawEnumitemLabel
    \item[Sol. for \( x \):] \lipsum[1]
    \item[Sol. for \( y \):] More blah blah blah.
    \end{itemize}

\noindent\rule{\linewidth}{0.4pt}

With \protect\verb|labelindent=\mylength|
\setlist[itemize]{
    noitemsep,
    topsep=0pt,
    parsep=4pt,
    partopsep=0pt,
    leftmargin=*,
    labelindent=52pt,
    }
\the\mylength

\begin{itemize}[left=\mylength]
    \DrawEnumitemLabel
    \item[Sol. for \( x \):] \lipsum[1]
    \item[Sol. for \( y \):] More blah blah blah.
    \end{itemize} 
\end{document}

在此处输入图片描述

在此处输入图片描述

相关内容