tcolorbox 和 tikz 节点中的精确尺寸

tcolorbox 和 tikz 节点中的精确尺寸

我在阿拉伯语文档中使用 tcolorbox 来处理定理示例的多语种,等等。我将环境标题放在 tikz 框中,放在页边距中(在右边距中,阿拉伯语以从右到左的模式书写)我希望(基本上是针对“小”示例,但也适用于其他示例...)获得框和节点的良好对齐。假设一个框和一个高度相同的节点。我手动完成,因为我不明白 tcolorbox 如何决定将节点准确放在哪里...我给出了一个 ECM。

tex 文件

    \documentclass[14pt]{extbook}%

    \usepackage[most]{tcolorbox}

    \usepackage{array}

     \tikzstyle{titlenode} =
     [draw=blue, thick, scale=.7, fill=red,% 
    line width=1pt, text=black, rectangle,
    font=\Large,
    left, minimum height=1cm]

    \newcounter{example}

    \newtcolorbox[auto counter,
        number within=section,
        number freestyle={\noexpand\arabic{section}\noexpand\alph{\tcbcounter}}]{example}[1][]{%
    enhanced,top=0mm,left=0pt,right=0pt,enlarge top by=\baselineskip/2+1mm,enlarge top at break by=0mm,pad at break=2mm,fontupper=\normalsize,
      overlay unbroken and first={%
        \pgfdeclarelayer{background}
        \pgfsetlayers{background,main}
    \node[titlenode] (B)
            at ([xshift=5mm,yshift=-6.3mm]frame.north east)
          {\strut\setlength{\extrarowheight}{-30.5pt}\textbf{$\begin{array}{c}
        \hline
        \rule{0pt}{2ex}
        \mbox{Example}
        \\
        \mbox{\Large\thetcbcounter}
        \rule{0pt}{1.5ex}
        \end{array}$}};
        },
    breakable,before=\par\smallskip\vspace{-10pt}\centering,arc=0pt,outer arc=0pt,colframe=white,colback=white,
    width=\textwidth,
    borderline north={2pt}{-3pt}{yellow},borderline south={2pt}{4.6pt}{blue},
    before upper={\parshape 4
            0pt \dimexpr \hsize-2.5cm\relax
            0pt \dimexpr \hsize-2.5cm\relax
            0pt \dimexpr \hsize-2.5cm\relax
            0pt \hsize},
    #1}

    \newtcolorbox[use counter from=example]{texample}[2][]{
    enhanced,boxrule=0pt,minipage boxed title=140mm,left=0pt,right=0pt,top=5mm,
    attach boxed title to top right={xshift=-5mm,yshift=-3.8mm,yshifttext=-1mm},
    coltitle=black,
    boxed title style={
    left=5pt,
    right=5pt,
    enhanced,
    arc=0pt,outer arc=0pt,
    colframe=blue,
    colback=yellow,
    colbacktitle=black,
    },
    enlarge top by=\baselineskip/2+1mm,enlarge top at break by=0mm,pad at break=2mm,fontupper=\normalsize,
      overlay unbroken and first={%
        \pgfdeclarelayer{background}
        \pgfsetlayers{background,main}
    \node[titlenode] (B)
            at ([xshift=5mm,yshift=-2.6mm]frame.north east)
          {\strut\setlength{\extrarowheight}{-30.5pt}\textbf{$\begin{array}{c}
        \hline
        \rule{0pt}{2ex}
        \mbox{Example}
        \\
        \mbox{\Large\thetcbcounter}
        \rule{0pt}{1.5ex}
        \end{array}$}};
        },
    breakable,before=\par\smallskip\centering,arc=0pt,colframe=white,colback=white,width=\textwidth,borderline south={2pt}{5pt}{blue},
    before upper={\parshape 2
            0pt \dimexpr \hsize-2.5cm\relax
            0pt \hsize},
    title={\strut\textbf{#2}\strut},
    #1}


    \begin{document}
    \setcounter{section}{1}

    \begin{example}
    A first example
    \end{example} 

    \vspace*{1cm}
    \begin{example}
    A first example. Aligned bottom
    \vspace*{.57cm}
    \end{example} 


    \begin{texample}{Titled example}
    By the same, I want that the title node rectangle will be exactly aligned with the box of title.
    \end{texample} 


    \end{document}

及其使用 xelatex 的编译

在此处输入图片描述

我希望标题节点矩形与北边界线(黄色)完全重叠。在示例 1a 中可以看到,框太小,我想将其也安排在底部,以便南边界线与标题框完全重叠。我在示例 1b 中手动安排它,在框的文本中使用命令 \vspace*{.57cm}。我希望自动执行此操作,即获得具有相同高度的框和节点。我该怎么做?

同样,我希望获得标题节点矩形和标题框的良好对齐。非常感谢

答案1

这可能会有帮助

\documentclass{book}

\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}

\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\newfontfamily{\arabicfont}[Script=Arabic,Scale=1.5]{Traditional Arabic}

\usepackage{polyglossia}
\setdefaultlanguage{arabic}

\newcommand{\testarab}{%
اللغة العربية لغة جميله
اللغة العربية لغة جميله
اللغة العربية لغة جميله
اللغة العربية لغة جميله
اللغة العربية لغة جميله}

\newcounter{example}[section]
\renewcommand{\theexample}{\thesection.\alph{example}}
\newcommand{\exemplelabel}{%
المثال
\LR{\theexample}}

% this
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\begin{tcolorbox}[enhanced,breakable,
colback=white, colframe=red, arc=0mm,
width=1.1\linewidth, rightrule=.1\linewidth, leftrule=0mm,
top=0mm, bottom=0mm, left=0mm, right=0mm,
colbacktitle=yellow, coltitle=black, title=#1,
overlay={%
\node[text width=.1\linewidth, align=center] at ([xshift=-.05\linewidth]frame.east) {\exemplelabel};}]}{%
\end{tcolorbox}}

% or this
\newenvironment{Example}[1][]{%
\refstepcounter{example}%
\begin{tcolorbox}[skin=enhancedmiddle,breakable,
colback=white, colframe=red, arc=0mm,
width=1.1\linewidth, rightrule=.1\linewidth, leftrule=0mm,
top=0mm, bottom=0mm, left=0mm, right=0mm,
colbacktitle=yellow, coltitle=black, title=#1,
overlay={%
\node[text width=.1\linewidth, align=center] at ([xshift=-.05\linewidth]frame.east) {\exemplelabel};
\draw [blue,line width=.5mm]([yshift=.25mm]frame.south west)--+(\linewidth,0);
\draw [blue,line width=.5mm]([yshift=-.25mm]frame.north west)--+(\linewidth,0);}
]}{%
\end{tcolorbox}}
%---------------------------------------------
\begin{document}
\testarab
\begin{example}[اللغة العربية]
\testarab
\end{example}

\begin{Example}[اللغة العربية]
\testarab
\end{Example}

\begin{example}
\testarab
\end{example}

\begin{Example}
\testarab
\end{Example}
\testarab

\end{document}

在此处输入图片描述

编辑对于固定高度我不确定,但你可以尝试这个

% this
\newenvironment{example}[1][]{%
\refstepcounter{example}%
\begin{tcolorbox}[enhanced,breakable,
colback=white, colframe=red, arc=0mm,
rightrule=.5mm, leftrule=0mm,
top=0mm, bottom=0mm, left=0mm, right=0mm,
colbacktitle=yellow, coltitle=black, title=#1,
overlay={%
\node[fill=red, below right, minimum height=3em, text width=1cm, align=center] at ([xshift=-.1mm]frame.north east) {\exemplelabel};}]}{%
\end{tcolorbox}}

% or this
\newenvironment{Example}[1][]{%
\refstepcounter{example}%
\begin{tcolorbox}[skin=enhancedmiddle,breakable,
colback=white, colframe=red, arc=0mm,
rightrule=.5mm, leftrule=0mm,
top=0mm, bottom=0mm, left=0mm, right=0mm,
colbacktitle=yellow, coltitle=black, title=#1,
overlay={%
\node[fill=red, below right, minimum height=3em, text width=1cm, align=center] at ([xshift=-.1mm]frame.north east) {\exemplelabel};
\draw [blue,line width=.5mm]([yshift=.25mm]frame.south west)--+(\linewidth,0);
\draw [blue,line width=.5mm]([yshift=-.25mm]frame.north west)--+(\linewidth,0);}
]}{%
\end{tcolorbox}}

相关内容