Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
share|improve this question
    
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
add comment

1 Answer 1

up vote 8 down vote accepted

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

\@makecaption:
\long macro:#1#2->\vskip \abovecaptionskip \sbox \@tempboxa {#1: #2}\ifdim \wd \@tempboxa >\hsize #1: #2\par
\else \global \@minipagefalse \hb@xt@ \hsize {\hfil \box \@tempboxa \hfil }\fi \vskip \belowcaptionskip

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.