1

In bold tikz text decoration |\bf| was mentioned as a possible way to insert a bolt tikz text decoration.

How to determine the width of a bold tikz text decoration?

I try:

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % ...                                                                                                                                                                                                     
  \pgfmathsetmacro{\mywidth}{width("Some text")}
  \pgfmathsetmacro{\mywidthb}{width("|\bf|Some bold text")}
  %...                                                                                                                                                                                                      
\end{tikzpicture}
\end{document}

The compilation encounters an error:

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.7 ...ro{\mywidthb}{width("|\bf|Some bold text")}

The application of the width will be as in centre text exactly on an exactly specified point on path

  • 1
    You can do \protected\def\mybold{\bfseries} and then \pgfmathsetmacro{\mywidthb}{width("|\mybod|Some bold text")}. – Henri Menke Jul 9 at 10:17
2

You can make this work by patching \pgfmathparse. You have to replace two \edef by \protected@edef. The following works:

\documentclass{article}
\usepackage{tikz}

\makeatletter

\def\pgfmathparse@#1{%
    % No (math) units yet.
    \global\pgfmathunitsdeclaredfalse
    \global\pgfmathmathunitsdeclaredfalse
    % Expand expression so any remaining CSs are registers
    % or box dimensions (i.e. |\wd|, |\ht|, |\dp|).
    \protected@edef\pgfmath@expression{#1}%
    %
    \expandafter\pgfmathparse@trynumber@loop\pgfmath@expression\pgfmath@parse@stop
    %
    % this here is the _real_ parser. it is invoked by
    % \pgfmathparse@trynumber@loop if that says "this is no number"
    %\pgfmathparse@@\pgfmath@parse@stop%
}

\def\pgfmath@startgroup@#1{\protected@edef\pgfmathresult{#1}}

\makeatother

\begin{document}
\pgfmathsetmacro{\mywidth}{width("Some text")}
\pgfmathsetmacro{\mywidthb}{width("|\bf|Some bold text")}
\end{document}
1

First of all, "|\bf|Some bold text" should, when used in decorations.text really be "|\bfseries|Some bold text", see this thread for more information, but as you point out neither of them work here. Even worse, the usual \setbox0\hbox{...} trick does not work since TikZ gobbles text, see e.g. this discussion, from which I construct a possible solution: interrupt the tikzpicture, measure, resume the tikzpicture. Of course one could use a different syntax and so on.

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\makeatletter
\newcommand{\IfInTikzPic}{% https://tex.stackexchange.com/a/121309/4301
  \ifx\pgfpictureid\@undefined
    \expandafter\@secondoftwo
     \else
    \expandafter\@firstoftwo
    \fi
}
\makeatother
% cf https://tex.stackexchange.com/a/459858/121799
\newcommand{\WidthOfStuff}[1]{\IfInTikzPic{\begin{pgfinterruptpicture}%
\setbox0\hbox{#1}%
\xdef\pgfmathresult{\the\wd0}%
\end{pgfinterruptpicture}}{%
\setbox0\hbox{#1}%
\xdef\pgfmathresult{\the\wd0}}}
\begin{document}
\begin{tikzpicture}
  % ...                                                                                                                                                                                                     
  \pgfmathsetmacro{\mywidth}{width("Some text")}
  \WidthOfStuff{\textbf{Some bold text}}
  \pgfmathsetmacro{\mywidthb}{\pgfmathresult}     

  \node{\mywidth,\mywidthb};
  %...                                                                                                                                                                                                      
\end{tikzpicture}
\end{document}
  • Tikz manual 3.1.3 on page 648 states that \bf can be used and gives an example of its use. I quote this page: It is possible to format the text using normal formatting commands, such as \it, \bf and \color, within customizable delimiters. – AndréC Jul 9 at 9:27
  • @AndréC Please read this comment. If you do not know these things, that's bad enough, but can happen. I didn't know that, too, until some years back. But the way you present the lack of knowledge in comments may be a reason for concern. – user121799 Jul 9 at 9:31
  • 1
    @AndréC LaTeX2e does not define \bf, \it, etc. in the format. The standard classes article, report, etc. define \bf, \it, etc. but they are not part of the kernel. – Henri Menke Jul 9 at 10:35
  • 1
    @marmot Inside a picture TikZ switches to \nullfont, but you can simply do \hbox{\selectfont...} to temporarily switch to a normal font. – Henri Menke Jul 9 at 10:49
  • 1
    @AndréC Try \bf\it and \bfseries\itshape. What is the expected output and what do these two output? On top of that \bf, \it, etc. mess with math families which comes with its own set of problem. – Henri Menke Jul 9 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.