我怎样才能使 `\newtcbox{}` 扩展到行尾?

我怎样才能使 `\newtcbox{}` 扩展到行尾?

我怎样才能\newtcbox{}扩展到行尾?这可能吗?定义宽度似乎没有任何效果...

使用\tcbox不是强制性的,但我希望该框是内联的。

在此处输入图片描述

\documentclass[a4paper,10pt]{article}

\usepackage{calc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}

\newlength{\AnswerLength}
\settowidth{\AnswerLength}{\textbf{Answer:~\null}}
\newcommand{\GrayColor}{gray!30}

\newtcbox{InlineAnswer}{%
    %top=0mm,
    %bottom=0mm,
    %left=0mm,
    %right=0mm,
    %width=\dimexpr\linewidth-\AnswerLength-\labelwidth-\labelsep\relax
    %capture=fitbox,
    %arc=0pt,
    %outer arc=0pt,
    nobeforeafter,
    tcbox raise base,
    boxsep=5pt,
    %boxrule=0.0pt,
    %colback=\GrayColor,
    %colframe=white,
}

\newcommand{\typeABinline}[2]{%
    \item #1 \\
    \textbf{Answer:} \InlineAnswer{\vphantom{Ap}\textbf{#2}}
}

\begin{document}

\begin{multicols}{2}

\begin{enumerate}[resume,label=\textbf{\arabic*.}]
    \typeABinline{Question text blah blah} {pe}
    \typeABinline{Question text blah blah} {asd}
    \typeABinline{Question text blah blah} {g}
    \typeABinline{Question text blah blah} {ka}
\end{enumerate}

\end{multicols}
\end{document}

答案1

除了指定等或等之外, Atcbox不能改变其宽度。right=...enlarge left by=...

但是,包装tcolorbox在命令内部也可以工作,请参见下面的示例。

这里有两个版本——带tcbraster和普通tcolorbox方式。raster版本更简洁,最有可能的是,应该tcbitemize使用外部itemize/enumerate结构。

\documentclass[a4paper,10pt]{article}

\usepackage{showframe}
\usepackage{calc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}



\newcommand{\GrayColor}{gray!30}


\makeatletter
\newtcolorbox{InlineAnswerBox}[1][]{%
    %top=0mm,
    %bottom=0mm,
    %left=0mm,
    %right=0mm,
  valign=center,
  halign=center,
  % capture=fitbox,
    %arc=0pt,
    %outer arc=0pt,
  nobeforeafter,
  boxsep=5pt,
  % boxrule=0.0pt,
  % colback=\GrayColor,
  % colframe=white,
  #1,
}
\makeatother

\newcommand{\InlineAnswer}[2][]{%
  \begin{InlineAnswerBox}[#1]
    #2
  \end{InlineAnswerBox}%
}

\newtcolorbox{answertextbox}[1][]{%
  valign=center,
  size=minimal,
  enhanced jigsaw, 
  boxrule=0pt, 
  sharp corners, 
  colback=white,
  nobeforeafter,
  left skip=0pt,
  right skip=0pt,
  #1
}

\renewcommand{\columnseprule}{2pt}

\newcommand{\typeABinline}[2]{%

\item #1 \\
  \begin{tcbraster}[nobeforeafter,raster  halign=center,raster columns=3,raster equal height group, raster column skip=0pt]
  \begin{answertextbox}[raster multicolumn=1,fontupper={\bfseries}]
    Answer: 
  \end{answertextbox} 
  \begin{InlineAnswerBox}[raster multicolumn=2,fontupper={\bfseries}]  % Use 2 fake columns!!
    #2
  \end{InlineAnswerBox}
\end{tcbraster}      
}

\begin{document}

\begin{multicols}{2}

\begin{enumerate}[resume,label=\textbf{\arabic*.}]
    \typeABinline{Question text blah blah} {pe}
    \typeABinline{Question text blah blah} {asd}
    \typeABinline{Question text blah blah} {g}
    \typeABinline{Question text blah blah} {ka}
\end{enumerate}

\end{multicols}
\end{document}

在此处输入图片描述

\documentclass[a4paper,10pt]{article}

\usepackage{calc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}



\newlength{\AnswerLength}
\settowidth{\AnswerLength}{\textbf{Answer:~\null}}
\newcommand{\GrayColor}{gray!30}

\makeatletter
\newtcolorbox{InlineAnswerBox}[1][]{%
    %top=0mm,
    %bottom=0mm,
    %left=0mm,
    %right=0mm,
  valign=center,
  halign=center,
  box align=center,
  baseline={\the\dimexpr\tcb@height/2-\kvtcb@top@rule\relax},
  width=\dimexpr\linewidth-\AnswerLength-\labelwidth-\labelsep\relax,
  % capture=fitbox,
    %arc=0pt,
    %outer arc=0pt,
  nobeforeafter,
  boxsep=5pt,
    %boxrule=0.0pt,
    %colback=\GrayColor,
    %colframe=white,
  #1,
}
\makeatother

\newcommand{\InlineAnswer}[2][]{%
  \begin{InlineAnswerBox}[#1]
    #2
  \end{InlineAnswerBox}%
}

\newcommand{\typeABinline}[2]{%
    \item #1 \\
    \textbf{Answer:} \InlineAnswer{\textbf{#2}}
}

\begin{document}

\begin{multicols}{2}

\begin{enumerate}[resume,label=\textbf{\arabic*.}]
    \typeABinline{Question text blah blah} {pe}
    \typeABinline{Question text blah blah} {asd}
    \typeABinline{Question text blah blah} {g}
    \typeABinline{Question text blah blah} {ka}
\end{enumerate}

\end{multicols}
\end{document}

答案2

另一个解决方案是缩小盒子,为答:文本并使用以下方式插入此文本\makebox

\documentclass[a4paper,10pt]{article}

\usepackage{calc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}


\newcommand{\typeABinline}[2]{%
  \item #1\\
  \setbox0=\hbox{\textbf{Answer: }}%
  \begin{tcolorbox}[nobeforeafter,left skip=\wd0]
    \makebox[0mm][r]{\box0\hspace{5.5mm}}\strut\textbf{#2}
  \end{tcolorbox}%
}

\begin{document}

\begin{multicols}{2}

\begin{enumerate}[resume,label=\textbf{\arabic*.}]
    \typeABinline{Question text blah blah} {pe}
    \typeABinline{Question text blah blah} {asd}
    \typeABinline{Question text blah blah} {g}
    \typeABinline{Question text blah blah} {ka}
    \typeABinline{Question text blah blah} {ka xx xx xx xx xx xx xx xx xx}
\end{enumerate}

\end{multicols}
\end{document}

得出:

在此处输入图片描述

相关内容