以下是这个问题的答案创建一个彩色盒子?由 Gonzalo Medina 提供我刚刚为练习列表做了一些安排代码是这样的
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins}
\usepackage{lipsum}
\newtcolorbox[auto counter,number within=section]{ejemplo}[1][]{
before=\bigskip\centering,
after=\bigskip,
breakable,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {{\small #1}};}}
\begin{document}
\begin{ejemplo}[Title]
\lipsum[2]
\end{ejemplo}
\end{document}
得到像这样的东西
但我需要添加另一个参数才能获得一些带有\tcblower
选项的框sidebyside
,换句话说,用上面的代码写一些类似的东西
\begin{ejemplo}[sidebyside]{Title}
\lipsum[2]
\tcblower
\lipsum[2]
\end{ejemplo}
我尝试过
\newtcolorbox[auto counter,number within=section]{ejemplo}[2][]{
before=\bigskip\centering,
after=\bigskip,
breakable,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {{\small #2}};},#1}
与
\newtcolorbox[auto counter,number within=section]{ejemplo}[2][]{
before=\bigskip\centering,
after=\bigskip,
breakable,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
before title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {title #2};},#1}
但两者都给了我错误我该如何实现
答案1
sidebyside
且不breakable
兼容。
改良版
在此版本中,该xparse
包(通过xparse
库)用于定义环境及其加星号的版本。无星号版本允许分页符,加星号版本使用sidebyside
;它们的使用方式如下:
\documentclass{article}
\usepackage[a6paper]{geometry}%jus for the example
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,xparse}
\usepackage{lipsum}
\DeclareTColorBox[auto counter,number within=section]{ejemplo}{sm}{
before=\bigskip\centering,
after=\bigskip,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,
fill=none},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {{\small #2}};
},
\IfBooleanTF{#1}{sidebyside}{breakable}
}
\begin{document}
\begin{ejemplo}*{Title}
Some test text
\tcblower
Some test text
\end{ejemplo}
\begin{ejemplo}{Title}
\lipsum[1-2]
\end{ejemplo}
\end{document}
结果:
使用
\DeclareTColorBox[auto counter,number within=section]{ejemplo}{smO{}}{
before=\bigskip\centering,
after=\bigskip,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,
fill=none},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {{\small #2}};
},
\IfBooleanTF{#1}{sidebyside}{breakable},
#3
}
现在标题后有一个可选参数来传递最终的键,例如
\begin{ejemplo}*{Title}[colback=orange!20]
Some test text
\tcblower
Some test text
\end{ejemplo}
\begin{ejemplo}{Title}[colback=green!20!black!40]
\lipsum[1-2]
\end{ejemplo}
初始版本
\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{skins}
\usepackage{lipsum}
\newtcolorbox[auto counter,number within=section]{ejemplo}[2][]{
before=\bigskip\centering,
after=\bigskip,
enhanced,
arc=15pt,
colframe=blue,
colback=white,
fonttitle=\sffamily\bfseries,
title=Ejem.~\thetcbcounter,
sharp corners,
rounded corners=northeast,
attach boxed title to top left,
boxed title style={
enhanced,
colframe=blue,
colback=cyan,
arc=5pt,
bottomrule=0pt,
rightrule=0pt,
sharp corners,
rounded corners=northeast,
},
interior style={},
frame style={
left color=blue,
right color=cyan,
fill=none
},
overlay unbroken and first={
\node[anchor=west,font=\sffamily,color=cyan]
at (title.east) {{\small #2}};
},
#1
}
\begin{document}
\begin{ejemplo}{Title}
\lipsum[2]
\end{ejemplo}
\begin{ejemplo}[sidebyside]{Title}
\lipsum[2]
\tcblower
\lipsum[4]
\end{ejemplo}
\end{document}
我添加了fill=none
以frame style
防止一些不想要的边界。感谢托马斯·F·斯特姆建议这个his comment
。