TransWikia.com

tikz/pgf: compute width of small text

TeX - LaTeX Asked by Mahkoe on April 4, 2021

In tikz/pgf, there is a function called width("x") . From the pgf manual, it returns:

the width of a (horizontal) TeX box containing x.

After this sentence it starts to talk about things I don’t understand:

The quote characters are necessary to prevent x from being parsed. It
is important to remember that any expression is expanded with edef
before being parsed, so any macros (e.g., font commands like tt or
Huge) will need to be “protected” (e.g., noexpandHuge is usually
sufficient).

I need to measure the width of some text with the small modifier. However, I do not understand at all what edef and noexpand do. I have tried all of the following combinations:

pgfmathsetmacro{mywidth}{width("{small My Text}")}
pgfmathsetmacro{mywidth}{width("{noexpandsmall My Text}")}
pgfmathsetmacro{mywidth}{width("small{My Text}")}
pgfmathsetmacro{mywidth}{width("noexpandsmall{My Text}")}
pgfmathsetmacro{mywidth}{width("small My Text")}
pgfmathsetmacro{mywidth}{width("noexpandsmall My Text")}

In all cases, the value of mywidth ends up being zero.

What am I doing wrong?

EDIT:

Here’s an MWE

documentclass{article}
usepackage{tikz}
usetikzlibrary{positioning}

    begin{document}
    
    begin{tikzpicture}
    
        pgfmathsetmacro{mywidthsmall}{width("{small My Text }")}
        pgfmathsetmacro{mywidthregular}{width("{My Text }")}
        node[draw] (node1) {mywidthsmall};
        node[draw, below = 0pt of node1] (node2) {mywidthregular};
    
    end{tikzpicture}
    
end{document}

This gives:

enter image description here

2 Answers

I get this using pgfmathsetmacro{mywidthsmall}{width("{small My Text }")}.

enter image description here

documentclass[a4paper]{article}
usepackage{tikz}
pgfmathsetmacro{mywidthsmall}{width("{small My Text }")} %<- added space
pgfmathsetmacro{mywidthhuge}{width("{huge My Text }")}   %<- added space
setlength{parindent}{0pt}

begin{document}
Width of {small My Text} = mywidthsmall

Width of {huge My Text} = mywidthhuge

bigskip

Try setting node width $ldots$

bigskip

begin{tikzpicture}

node[text width=mywidthsmall,font=small,align=left,inner sep=0pt] at (0,0) {My Text};
node[text width=mywidthsmall,font=small,inner sep=0pt] at (0,-1) {My Text My Text};

node[text width=mywidthhuge,font=huge,inner sep=0pt] at (0,-3) {My Text};
node[text width=mywidthhuge,font=huge,inner sep=0pt] at (0,-5) {My Text My Text};

end{tikzpicture}
end{document}

Answered by Ross on April 4, 2021

I don't got what is the deeper sense here. But widt("abc") gives a point value, because width is a measure of length and by definition it has a unit of length, in this case points.

So it is normally needed pgfmathsetlenghtmacroname{widt("abc")}, if this width shall be used somewhere, which gives the length e.g. 12.34pt
and lesser pgfmathsetmacroname{widt("abc")} which erases the unit points and gives the number e.g. 12.34

Whatever:

enter image description here

documentclass[a4paper]{article}
usepackage{tikz}
defsampletext{{tiny My Text}}
defSampletext{{Huge My Text}}
begin{document}
section{tiny}
pgfmathsetlengthmacro{mywidth}{width("sampletext")} 
sampletext~ has the width mywidth

section{Huge}
pgfmathsetlengthmacro{Mywidth}{width("Sampletext")} 
Sampletext~ has the width Mywidth

section{pgfmathsetemph{length}macro}
begin{tikzpicture}[
mystyle/.style={align=left,inner sep=0pt, anchor=west, draw}
]
node[mystyle, draw, text width=mywidth+1pt] (textbox) at (0,0) {sampletext};
draw[red] (textbox.north west) -- +(mywidth,0) node[right=1mm]{mywidth+1pt};

node[mystyle, text width=Mywidth+0pt] (textbox) at (0,-1) {Sampletext};
draw[red] (textbox.north west) -- +(Mywidth,0) node[right=1mm]{Mywidth};
end{tikzpicture}

section{Let's ruin it with pgfmathsetmacro, without emph{length}}
pgfmathsetmacro{Mywidth}{width("Sampletext")} 
begin{tikzpicture}[
mystyle/.style={align=left,inner sep=0pt, anchor=west, draw}
]
node[mystyle, text width=Mywidth+0pt] (textbox) at (0,-1) {Sampletext};
draw[red] (textbox.north west) -- +(Mywidth,0) node[right=1mm]{Mywidth};
end{tikzpicture}

Box correct, because texttt{text width=123.4} (without unit)  sets points, as one would have written texttt{text width=123.4pt}. par 
Draw worse, because the default unit of TikZ is texttt{cm}. 
end{document}

Answered by cis on April 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP