仅在 Tcolorbox 中的框标题周围添加框架。制作“半背景”

仅在 Tcolorbox 中的框标题周围添加框架。制作“半背景”

用阿拉伯语写一本书,使用多语种,我使用 Tcolorbox 制作例子、练习、定理等。我有两种标题示例:一种是阿拉伯语标题,在 RighToLeft 模式下,通过选项获得

  title=\RL{\textbf{#2}}

另一个有两个标题,一个是阿拉伯语,一个是英语,通过选择获得

  title=\RL{\textbf{#2}}}\hfill{\small\LR{\textbf{#3}}

我使用命令“colbacktitle=gray!20”作为标题的背景,我想要两件事:

1 / 首先,对于唯一的阿拉伯语标题,我想要获得该标题的“半框”,因此框标题的宽度等于示例框的半宽。

2/ 我想要的另一件事是为 tcolorbox 制作一个框架,但只围绕示例的标题

谢谢你的帮助

我编辑了该问题并添加了 MWE:

\documentclass[14pt,twoside]{book}

\usepackage{extsizes}

\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage{array}
\usepackage[total={165mm,235mm},centering,twoside]{geometry}% 

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}
\newfontfamily\arabicfont[Script=Arabic,Scale=1.2,AutoFakeSlant=-0.02]{Simplified Arabic}
\setmonofont{DejaVu Sans Mono}
\let\arabicfonttt\ttfamily
\setsansfont[Script=Arabic,Scale=1.5]{Simplified Arabic}

\tikzstyle{whitebox} =
[draw=white, thick, scale=.707, fill=white,% 
line width=0.3pt, text=black, rectangle,
font=\Large,
left, minimum height=.5cm, minimum width=1.9cm]

\newcounter{example}

\newtcolorbox[auto counter, 
number within=section,
number freestyle={\noexpand\arabic{\tcbcounter}}]{example}[2][]{%
enhanced,
left=-5pt,
right=-5pt,
colback=white,
colframe=white,
top=5pt,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#2},
overlay unbroken and first={%
\node[whitebox]
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
#1}%


\newtcolorbox[use counter from=example]{texample}[3][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\mbox{\hspace*{5pt}\RL{\textbf{#2}}},
colback=white,
colframe=red,
top=0mm,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
enlarge top  by=-2mm,
fontupper=\normalsize,
label={#3},
  overlay unbroken and first={%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[whitebox] (B)
  at ([xshift=1.5cm,yshift=-1cm]frame.north east)
  {\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
 $\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
  extrude right by=5pt,
  extrude left by=5pt,
  enlarge top at break by=0mm,pad at break=2mm,
  #1%
}%

\newtcolorbox[use counter from=example]{teaexample}[4][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\hspace*{5pt}\RL{\textbf{#2}}\hfill\begin{english}\LR{\textbf{#3}}\end{english},
colback=white,
colframe=red,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#4},
  overlay unbroken and first={%
\node[whitebox] (B)
  at ([xshift=1.5cm,yshift=-1cm]frame.north east)
  {\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
  extrude right by=5pt,
  extrude left by=5pt,
  enlarge top at break by=0mm,pad at break=2mm,
  #1%
}%


\begin{document}


\begin{example}{l1}
\LR{The text of the example (labeled here Ex) is usually in arabic, in RightToLeft mode}
\end{example}

\begin{texample}{\LR{Arabic title1}}{l2} 
\LR{Text of another example}
\end{texample}
\begin{teaexample}{\LR{Arabic title2}}{English title}{l3} 
\LR{Another text of another example}
\end{teaexample}
\end{document}

我给出了这个文件的编译(必须用 xelatex 编译器完成)

在此处输入图片描述

对于第二个例子,我想要一个宽度为总宽度一半的框标题(所以是 82.5 毫米)

其他事情。是否可以只在标题框周围添加框架(此处为红色)(此处为灰色)?

答案1

将 设置boxrule0pt(因此整个框不绘制任何框架)并使用其中一个选项附加标题,例如:

boxrule=0pt,
minipage boxed title=\textwidth,
attach boxed title to top right,
boxed title style={
  enhanced,
  arc=0pt,
  outer arc=0pt,
  colbacktitle=gray!40,
  colframe=red,
  }

使用minipage boxed title=<width>您可以控制盒装标题所需的宽度,然后您可以使用boxed title style进一步自定义用于标题的框。

完整示例(根据需要调整设置):

\documentclass[14pt,twoside]{book}

\usepackage{extsizes}

\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage{array}
\usepackage[total={165mm,235mm},centering,twoside]{geometry}% 

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}
\newfontfamily\arabicfont[Script=Arabic,Scale=1.2,AutoFakeSlant=-0.02]{Scheherazade}
\setmonofont{DejaVu Sans Mono}
\let\arabicfonttt\ttfamily
\setsansfont[Script=Arabic,Scale=1.5]{Scheherazade}

\tikzstyle{whitebox} =
[draw=white, thick, scale=.707, fill=white,% 
line width=0.3pt, text=black, rectangle,
font=\Large,
left, minimum height=.5cm, minimum width=1.9cm]

\newcounter{example}

\newtcolorbox[auto counter, 
number within=section,
number freestyle={\noexpand\arabic{\tcbcounter}}]{example}[2][]{%
enhanced,
left=-5pt,
right=-5pt,
colback=white,
colframe=white,
top=5pt,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#2},
overlay unbroken and first={%
\node[whitebox]
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
#1}%


\newtcolorbox[use counter from=example]{texample}[3][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\mbox{\hspace*{5pt}\RL{\strut\textbf{#2}\strut}},
colback=white,
boxrule=0pt,
minipage boxed title=0.5\textwidth,
attach boxed title to top right,
boxed title style={
  enhanced,
  arc=0pt,
  outer arc=0pt,
  colbacktitle=gray!40,
  colframe=red,
  },
top=0mm,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
enlarge top  by=-2mm,
fontupper=\normalsize,
label={#3},
  overlay unbroken and first={%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[whitebox] (B)
  at ([xshift=1.5cm,yshift=-1cm]frame.north east|-title.north east)
  {\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
 $\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
  extrude right by=5pt,
  extrude left by=5pt,
  enlarge top at break by=0mm,pad at break=2mm,
  #1%
}%

\newtcolorbox[use counter from=example]{teaexample}[4][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\hspace*{5pt}\RL{\strut\textbf{#2}}\hfill\begin{english}\LR{\textbf{#3}\strut}\end{english},
colback=white,
boxrule=0pt,
minipage boxed title=\textwidth,
attach boxed title to top right,
boxed title style={
  enhanced,
  arc=0pt,
  outer arc=0pt,
  colbacktitle=gray!40,
  colframe=red,
  },
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#4},
  overlay unbroken and first={%
\node[whitebox] (B)
  at ([xshift=1.5cm,yshift=-1cm]frame.north east|-title.north east)
  {\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c}
\hline
\rule{0pt}{2ex}
\mbox{Ex}
\\
\mbox{\large\thetcbcounter}
\rule{0pt}{1.5ex}
\end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
  extrude right by=5pt,
  extrude left by=5pt,
  enlarge top at break by=0mm,pad at break=2mm,
  #1%
}%


\begin{document}


\begin{example}{l1}
\LR{The text of the example (labeled here Ex) is usually in arabic, in RightToLeft mode}
\end{example}

\begin{texample}{\LR{Arabic title1}}{l2} 
\LR{Text of another example}
\end{texample}
\begin{teaexample}{\LR{Arabic title2}}{English title}{l3} 
\LR{Another text of another example}
\end{teaexample}
\end{document}

在此处输入图片描述

评论

  • \strut在标题中添加了 s,这样无论有没有下降部/上升部,框都会具有相同的高度。

  • “Ex #”标签的位置现在根据frametitle“盒子”而定,因此它与新定义的使用一致。

  • 我将阿拉伯字体改为 Sheherazade,因为我没有您使用的字体,但这与解决方案无关。

相关内容