![使用 Tcolorbox 确定居中框的宽度](https://linux22.com/image/289033/%E4%BD%BF%E7%94%A8%20Tcolorbox%20%E7%A1%AE%E5%AE%9A%E5%B1%85%E4%B8%AD%E6%A1%86%E7%9A%84%E5%AE%BD%E5%BA%A6.png)
我正在写一本书,我想使用盒子,使用包tcolorbox
。我在这里的一些旧讨论中找到了命令,\newtcolorbox{blankbox}
使用命令width=0.9\textwidth
,它会产生一个居中的盒子,但宽度为 0.9。当我将宽度设置为另一个值时,它不居中。我的问题是,我如何才能获得一个始终与页面居中的盒子,并且宽度是我想要的?
我也遇到了另一个问题。有时框的规则不显示……有时是上方的,有时是下方的,有时是右侧的,有时是左侧的(就像这里的第二个框,用阿拉伯语写成的)。这是什么原因?
我在这里给出一个最简单的例子,其中包含我的书中的软件包:
\documentclass[14pt,a4paper]{extbook}%{article}%
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{multicol}
\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage{boxedminipage}
\usepackage{slashbox}
\usepackage{titlesec}
\usepackage{enumitem}
\usepackage{minitoc}
\usepackage{rotating}
\usepackage{fmultico}
\setlength{\columnseprule}{0.1pt}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{pdflscape}
\usepackage{array}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage[numerals=maghrib]{arabic}
\newfontfamily\arabicfont[Script=Arabic, AutoFakeSlant=-0.02]{Amiri}
\setsansfont[Script=Arabic,Scale=1.5]{Amiri}
\tikzstyle{titlewhite} =
[draw=black, thick, scale=.7, fill=white,%
line width=0.1pt, text=black, rectangle,
font=\Large,
left, minimum height=.5cm]
\newtcolorbox{blankbox}[2][]{%
enhanced,
%oversize,
colback=white,
boxrule=.5pt,
colframe=black,
top=6mm,
bottom=6mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
overlay unbroken and first={%
\node[titlewhite]
at ([xshift=-1cm]frame.north east)
{\strut\RL{\textbf{#2}}};},
breakable,
width=0.9\textwidth, %%% change the width here.
arc=0pt,outer arc=0pt,
%enlarge left by=-.06\textwidth,
extrude right by=-5pt,
extrude left by=-5pt,
#1}%
\newtcolorbox{blankbox1}[2][]{%
enhanced,
%oversize,
colback=white,
boxrule=.5pt,
colframe=black,
top=6mm,
bottom=6mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
overlay unbroken and first={%
\node[titlewhite]
at ([xshift=-1cm]frame.north east)
{\strut\RL{\textbf{#2}}};},
breakable,
width=\textwidth, %%% change the width here.
arc=0pt,outer arc=0pt,
%enlarge left by=-.06\textwidth,
extrude right by=-5pt,
extrude left by=-5pt,
#1}%
\begin{document}
\begin{blankbox}{title}
I want to obtain a box having the same width of the text, and centered as the text... More precisely, in which point do tcolorbox puts the box exactly??... And how can I obtain, a box always centered as the page, and with the width what I want ??
I have another problem too... sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the right, sometimes on the left... (like in the second box here, written in arabic)...what's the reason for this ?
\end{blankbox}
\begin{blankbox1}{title}
I want to obtain a box having the same width of the text, and centered as the text... More precisely, in which point do tcolorbox puts the box exactly??... And how can I obtain, a box always centered as the page, and with the width what I want ??
I have another problem too... sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the right, sometimes on the left... (like in the second box here, written in arabic)...what's the reason for this ?
\end{blankbox1}
\end{document}
答案1
您可以使用宏定义一个居中框\centering
,如下所示:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tcolorbox}
\newtcolorbox{cbox}[1][]{
before=\par\smallskip\centering,
#1
}
\begin{document}
some text before
\begin{cbox}[width=.9\textwidth]
test
\end{cbox}
some text after
\begin{cbox}[width=.8\textwidth]
test
\end{cbox}
\begin{cbox}[width=.7\textwidth]
test
\end{cbox}
\begin{cbox}[width=.6\textwidth]
test
\end{cbox}
\begin{cbox}[width=.5\textwidth]
test
\end{cbox}
\end{document}
设置居中框模板后,您可以指定文档内每个框的宽度,并且它们都将居中。