# Test if text fits on one line

I have a boxed text that is justified, but I would like to make it centered if it takes only one line. Currently, once the text is rendered, I add a \filcenter as an option to my box macro, but I'm wondering if I could do that automatically.

Edit:

My current code is :

% central quote
\newcommand{\dvbox}[2][]{%
\begin{center}\doublebox{%
\parbox{10cm}{%
\vspace{3mm}%
\begin{Center}%
\makeatletter
\@raggedtwoe@spaceskipfalse
\@raggedtwoe@everyselectfont
\makeatother
\parbox{9cm}{#1\textsc{#2}}%
\end{Center}%
\vspace{3mm}%
}
}\end{center}}


Currently, I pass \filcenter as #1 for single lines, and #2 is the text to display.

@Martin: so your solution gives me this:

\makeatletter
% justify or center
\newsavebox{\@justcentbox}%
\newcommand{\justifyorcenter}[1]{%
\sbox \@justcentbox{#1}%
\ifdim \wd \@justcentbox >\hsize #1%
\else \centerline{#1} \fi
}

% central quote
\newcommand{\dvbox}[2][]{%
\begin{center}\doublebox{%
\parbox{10cm}{%
\vspace{3mm}%
\begin{Center}%
\@raggedtwoe@spaceskipfalse
\@raggedtwoe@everyselectfont
\parbox{9cm}{\justifyorcenter{#1\textsc{#2}}}%
\end{Center}%
\vspace{3mm}%
}
}\end{center}}
\makeatother

-
How is the Center environment defined? You need to place \makeatletter/\makeatother outside the \dvbox definition. They must be active when the macro is defined, not when it is used. –  Martin Scharrer Apr 21 '11 at 13:01
The Center environment is from ragged2e. –  ℝaphink Apr 21 '11 at 13:03
You updated code looks rights. You could use the box using \usebox{\@justcentbox} in the else branch instead of #1, which is more efficient. –  Martin Scharrer Apr 21 '11 at 13:05
Thank you very much. –  ℝaphink Apr 21 '11 at 13:09

The standard \caption macro shows this behaviour. The formatting is actually done by the internal \@makecaption macro:
\@makecaption:

So: Store the content in a box (e.g. using \savebox\yourbox{<content>}, measure the width using \ifdim\wd\yourbox>\hsize (or \linewidth) and then center it or not. In the second case you need to typeset the content a second time, while in the first you can reuse the saved box. This simplest thing to center one line is \centerline{...} which is very similar to the code \@makecaption uses.
Very interesting thanks. To clarify things, I will add my current code to the question. Should I put the test part inside the \parbox{9cm} part? –  ℝaphink Apr 21 '11 at 12:32