获取节点中粗体文本的宽度

获取节点中粗体文本的宽度

我正在为定理创建一个框,我需要计算标题的长度以剪切框架的顶部以免覆盖我的标题。代码可以工作,但如果我替换#3 \arabic{#1}##1宽度则不正确并且我的标题被覆盖。当文本在或时\textbf{\textsc{#3} \arabic{#1}##1},我如何测量文本的正确宽度?textbftextsc

梅威瑟:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage{afterpage}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{rednew}{RGB}{178,27,15}
\begin{document}

\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
    \newcounter{#1}
    \newenvironment{#1}[1][]{
        \refstepcounter{#1}
        \begin{tcolorbox}[enhanced,opacityback=0,attach boxed title to top left={yshift=-\tcboxedtitleheight/2,xshift=4mm},center title,opacityframe=0,before skip=0.5cm,left skip=0.16cm,left=0.2cm,right=0cm,top=0.2cm,bottom=0cm,breakable,boxed title style={opacityback=0,opacityframe=0,colframe=white,size=fbox,arc=0mm},overlay unbroken and first={\pgfmathsetmacro{\textPosX}{width("#3 \arabic{#1}##1")*1pt/1cm}
        \node[anchor=west] at ($(interior.north west)+(0.345,0.05)$){#3 \arabic{#1}##1\vphantom{/Î)}};
        \draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\textPosX,0)+(0.215,0)$);},overlay middle and last={\draw[very thick](frame.north west)--(frame.south west);},after={\vspace{1ex} \noindent}]}
{\end{tcolorbox}}}
        {\newenvironment{#1}[1][]{
       \begin{tcolorbox}[enhanced,opacityback=0,attach boxed title to top left={yshift=-\tcboxedtitleheight/2,xshift=4mm},center title,opacityframe=0,before skip=0.5cm,left skip=0.16cm,left=0.2cm,right=0cm,top=0.2cm,bottom=0cm,breakable,boxed title style={opacityback=0,opacityframe=0,colframe=white,size=fbox,arc=0mm},overlay unbroken and first={\pgfmathsetmacro{\textPosX}{width("#3##1")*1pt/1cm}
        \node[anchor=west] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3}##1}\vphantom{/Î)}};
        \draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\textPosX,0)+(0.215,0)$);},overlay middle and last={\draw[very thick](frame.north west)--(frame.south west);},after={\vspace{1ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{theoremtest}{red}{Theorem name}{o}

\begin{theoremtest}
test
\end{theoremtest}
\end{document}

答案1

评论后修改

            \documentclass{article}
    \begin{document}
    \newlength{\longueurA}
    \settowidth{\longueurA}{Theorem name}
    \the\longueurA

    \newlength{\longueurB}
    \settowidth{\longueurB}{\textbf{\textsc{Theorem name}}}
    \the\longueurB
    %https://tex.stackexchange.com/questions/41370/what-are-the-possible-dimensions-sizes-units-latex-understands/41371#41371
    %
    %https://tex.stackexchange.com/questions/8260/what-are-the-various-units-ex-em-in-pt-bp-dd-pc-expressed-in-mm
    \makeatletter
    \def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
    \makeatother

    \convertto{cm}{\longueurB}cm
    \end{document}

修改了 30/05/22 的转换,使用 expl3

    \documentclass{article}
    % the doc The LATEX3 Interfaces
    % section 25.7 Using dim expressions and variables
    \ExplSyntaxOn
    \NewDocumentCommand{\myconversion}{m}{%
        \dim_to_decimal_in_unit:nn { #1 } { 1cm }
    }
    \ExplSyntaxOff
    \begin{document}
    \newlength{\longueurB}
    \settowidth{\longueurB}{\textbf{\textsc{Theorem name}}}
    \the\longueurB

    \myconversion{\longueurB}cm
    \end{document}

相关内容