2

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.

  • 2
    Probably it is more convenient to use the »mdframed« package here. – Thorsten Donig Jul 17 '13 at 7:26
  • 2
    Or even better tcolorbox. – user11232 Jul 17 '13 at 7:28
  • I tried mdframed but I couldn't get it right either, what's wrong with using tikz? sorry I'm still a noob in Latex, thanks. – maraujop Jul 17 '13 at 7:51
  • Nothing against using »PGF/TikZ«. But I would not try to reinvent the wheel but resort to a package that exactly covers the problem. – Thorsten Donig Jul 17 '13 at 8:23
  • 3
    Don't think Tikz is relevant, check this question and associated answers: tex.stackexchange.com/questions/48276/… – Roelof Spijker Jul 17 '13 at 8:28
1

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

|improve this answer|||||

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.