# 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