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 would like to define a new environment that centers its contents if it can be typeset on a single line, but justifies the contents if it requires multiple lines.

\documentclass{article}
\usepackage{lipsum}
\newenvironment{mycenter}{\noindent}{}
\begin{document}

\begin{mycenter}%
This should be centered since it is a single line
\end{mycenter}

\begin{mycenter}%
This should be justified since it is multiple lines. \lipsum[1]
\end{mycenter}

\end{document}

I have a convoluted idea using the Environ package to capture the contents, then sticking them in a box and trying to check the height of the box. Is there an easier way?

share|improve this question
1  
LaTeX's default \caption macro does exactly this. It works, but it may be that you're bound to typeset things more than once. Therefore, you have to be careful if the contents includes counter stepping or perhaps labels. –  Werner Feb 6 '13 at 15:10
    
@Werner: Why not creating a box with the contents and centering the box if its width is less than \textwidth or \linewidth? –  krlmlr Feb 6 '13 at 15:40
    
@krlmlr: The problem doesn't lie there. It lies with the resetting of the contents if it's greater than \textwidth, since setting it in a box doesn't have any text boundary and would require re-setting if wider than the text block. The caption package apparently circumvents this. –  Werner Feb 6 '13 at 15:43
    
@Werner: I see the issue for multiple paragraphs that all fit the desired width, but I'm not sure if that is a requirement. Single paragraphs can be typeset in a box justified and with restricted width; this box then can be centered if its width is smaller. –  krlmlr Feb 6 '13 at 15:51
    
environ is what you're looking for. I'd measure the width, rather than the height. –  egreg Feb 6 '13 at 15:55
add comment

1 Answer

up vote 3 down vote accepted

enter image description here

\documentclass{article}
\usepackage{lipsum}
\newenvironment{mycenter}{\vbox\bgroup\bgroup
\parindent0pt\relax}{%
\par
\ifnum\prevgraf=1
\setbox0\lastbox
\noindent\hskip\parfillskip\hbox{\unhbox0}\par
\fi
\egroup\egroup}
\begin{document}

\begin{mycenter}%
This should be centered since it is a single line
\end{mycenter}

\begin{mycenter}%
This should be justified since it is multiple lines. \lipsum[1]
\end{mycenter}

\end{document}
share|improve this answer
    
This is awesome! Why a double grouping? Also adding preceding and ending \strut makes for better alignment, no? –  Werner Feb 6 '13 at 18:16
    
@Werner double grouping needed for colour safety (see grfguide) whether you need struts depends a bit on what you do with the box, whether you unbox it back on to the list or not. –  David Carlisle Feb 6 '13 at 19:36
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.