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

Viesturs
  • 7,895
  • 1
    You can do \protected\def\mybold{\bfseries} and then \pgfmathsetmacro{\mywidthb}{width("|\mybod|Some bold text")}. – Henri Menke Jul 09 '19 at 10:17

2 Answers2

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}
Henri Menke
  • 109,596
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 09 '19 at 09: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. –  Jul 09 '19 at 09:31
  • I would like to believe that Phelype Oleinik is right. But like any scientist, I never take anyone's word for it. Is there an official source from the LaTeX core development team confirming Phelype's statement? – AndréC Jul 09 '19 at 09:42
  • @AndréC Scientists depend on academic honesty... But yes, see https://tex.stackexchange.com/q/516/121799. –  Jul 09 '19 at 09:44
  • I don't understand your remark about academic honesty. Joseph Wright pointed out that these commands are supported. And moreover, TikZ is written entirely in Plain TEX, its source code is located in the tex/generic/pgf folder like all programs written in TeX and not in LaTeX. Pgf-tikz can be compiled into Plain TeX. – AndréC Jul 09 '19 at 09:55
  • @AndréC Discussing with you is a pain. Is the OP's code plain TeX, yes or no? You asked why I say that one should use \bfseries, and I gave you several reasons. If you do not want to look at them, this is your private choice. But please do not add these spam-like comments on posts that attempt to solve problems. –  Jul 09 '19 at 10:11
  • 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 09 '19 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 09 '19 at 10:49
  • @HenriMenke Yes, that is exactly what Joseph Wright said here Does it matter if I use \textit or \it, \bfseries or \bf, etc and I quote: The \it syntax is inherited from LaTeX 2.09, and is regarded as supported for historical reasons only in LaTeX2e. For bold, you should go for \textbf rather than \bf. He doesn't mention the need to use \bfseries. So what about this? – AndréC Jul 09 '19 at 10:57
  • @AndréC Because the “LaTeX way” is passing arguments, e.g. \frac{a}{b} instead of {a \over b} and therefore \texbf{...} instead of {\bfseries ...}. – Henri Menke Jul 09 '19 at 11:01
  • @HenriMenke Okay, the way to pass the arguments is not the same. As a TikZ developer, how do you explain that both syntaxes work? Is there an advantage or disadvantage to using one syntax instead of the other? – AndréC Jul 09 '19 at 11:09
  • 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 09 '19 at 11:12
  • @HenriMenke After various attempts, I note that the syntax \it\bf does not put the text in italics. Similarly, \bf\it does not put the text in bold while \itshape\bfseries and \bfseries\itshape put the text in italics and bold. Thus, I conclude that the new syntax is more flexible for text formatting. Thank you for your clear explanations. – AndréC Jul 09 '19 at 11:28
  • @AndréC Well, this is all discussed at length under https://tex.stackexchange.com/q/516/121799... –  Jul 09 '19 at 11:45
  • @marmot This does not prevent the syntax \bf is allowed with TikZ despite its lack of flexibility. – AndréC Jul 09 '19 at 13:32
  • @HenriMenke Is there a specific order to the formatting of the text? In other words, can we write \footnotesize\bfseries\itshape as well as \bfseries\footnotesize\itshape or are there rules to follow in the successive call of these macros? – AndréC Jul 09 '19 at 14:13