[Tex/LaTex] tikz setting font size to pts

pdftextikz-pgf

I'm using tikz to create color boxes containing text. However I haven't been able to set font size within these boxes using pts.

I found a snippet in this question that defines a Boxed command:

\newcommand{\Boxed}[2][]{%
    % #1 = box draw/fill options
    % #2 = text
    \settowidth{\LengthOfText}{\mbox{#2}}%
    \pgfmathsetlength{\LengthOfTextExceedingLineWidth}
        {\LengthOfText-\linewidth}
    \pgfmathsetlength{\TextWidth}{\LengthOfTextExceedingLineWidth > 0pt ? \linewidth : \LengthOfText}%
    \begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
            \tikzstyle{every node}=[font=\small]
            \node [text width=\TextWidth, #1] (Origin) {#2};
                (Origin.south west) rectangle (Origin.north east) ;
    \end{tikzpicture}%
}

I've modified it a little, however using a tikzstyle I'm only able to set font to things like \small, \tiny, etc. I would like to set font size to 10pt.

Best Answer

One way to get the desired font size, since you don't mind the result in a box (which means no line or page breaking} is to just scale it by a factor of desired-point-size/document-point-size.

In this MWE, the document is 12pt, while I want the \Boxed in 10pt, so I use a \scalebox with a scale of 10/12 = 0.8333

\documentclass[12pt]{article}
\usepackage{tikz}
\newlength{\LengthOfText}
\newlength{\LengthOfTextExceedingLineWidth}
\newlength{\TextWidth}
\newcommand{\Boxed}[2][]{%
    % #1 = box draw/fill options
    % #2 = text
    \settowidth{\LengthOfText}{\mbox{#2}}%
    \pgfmathsetlength{\LengthOfTextExceedingLineWidth}
        {\LengthOfText-\linewidth}
    \pgfmathsetlength{\TextWidth}{\LengthOfTextExceedingLineWidth > 0pt ? \linewidth : \LengthOfText}%
    \begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
            \tikzstyle{every node}=[font=\small]
            \node [text width=\TextWidth, #1] (Origin) {#2};
                (Origin.south west) rectangle (Origin.north east) ;
    \end{tikzpicture}%
}
\begin{document}
Normal text in 12pt font.  \scalebox{.8333}{\Boxed[blue]{10pt font}}
\end{document}

enter image description here

Related Question