[Tex/LaTex] TikZ node as character in text

symbolstikz-pgf

I use a modified version of the TikZ-solution in another question to draw rounded rectangles around some text to mark it as a button of a program. It's no problem for single characters, but if I use longer terms (as the buttons are named in my program) I get problems with Overfull \hboxes.

MWE:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{tikz}

\newcommand*\rectangled[1]{\tikz[baseline=(char.base)]{
    \node[shape=rectangle,draw,inner sep=2pt, rounded corners=4pt, thick] (char) {\sffamily{#1}};}}

\begin{document}
  A rectangled number \rectangled{1}: No problem normally as it is used as normal charakter and TeX can set it in the right position.

  A button with a longer name is marked using the same command and \rectangled{creates an Overfull \textbackslash hbox} if it is set to the end of a line.
\end{document}

The produced output looks like this:
Output of MWE

Is there a better solution to avoid such Overfull \hboxes than rearrange the sentence until it produces no problem anymore?

Best Answer

A sloppypar will momentarily allow you to overcome the problem. This problem arises for any oversized box that wants to be typeset at the end of a line.

The sloppypar feature (or \sloppy for the whole document) changes TeX's penalties to give more emphasis to avoiding margin overruns, at the expense of grossly wide interword spaces. There is no "free lunch."

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{tikz}

\newcommand*\rectangled[1]{\tikz[baseline=(char.base)]{
    \node[shape=rectangle,draw,inner sep=2pt, rounded corners=4pt, thick] (char) {\sffamily#1};}}

\begin{document}
  A rectangled number \rectangled{1}: No problem normally as it is used as normal charakter and TeX can set it in the right position.

\begin{sloppypar}
  A button with a longer name is marked using the same command and \rectangled{creates an Overfull \textbackslash hbox} if it is set to the end of a line.
\end{sloppypar}
\end{document}

enter image description here

Related Question