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 have a tabular or something similar with one cell and the following behavior:

  • The cell grows vertically and horizontally to fit the content of the cell but no more than \linewidth
  • The lines break instead of going out of the cell or the cell itself growing bigger than the page

So far I've only found two partial solutions :

  • Using a simple tabular with p{.?\textwidth} to ensure line breaks and vertical grow but that does not provides me with a box growing horizontally
  • Using a simple tabular with l, r or c to ensure automated horizontal grow

If you wonder why I want a single cell table, its because I need some sort of "box" that can have borders and a background color.

share|improve this question
3  
This is what the standard latex classes do for captions, they set it first in a box (so it is all on one line) then measure the width and if that is small the box is used as is, if it is too wide that box is discarded and the contents are re-set into a parbox of width textwidth. This works OK but evaluating the argument twice has some side effects if anything writes to files or increments counters. –  David Carlisle Feb 20 '12 at 20:04
    
@DavidCarlisle: Do you know whether the same holds for the varwidth package? Setting, testing and possibly resetting, I mean? –  Werner Feb 20 '12 at 20:11
    
just saw your answer as I posted mine. Will look, never seen that before –  David Carlisle Feb 20 '12 at 20:14
2  
@Werner, oh it's one of Donald's. varwidth only executes things once, but then measures and reboxes things rather than re-executing the token list. So it is generally better (but rather more work than my 6-liner:-) so I'll give you a vote:-) –  David Carlisle Feb 20 '12 at 20:21
    
@DavidCarlisle: Oh, you're too kind. :) –  Werner Feb 20 '12 at 20:24
add comment

3 Answers

up vote 4 down vote accepted

You could set the cell content in a varwidth environment (from the varwidth package). Here's a minimal example:

enter image description here

\documentclass{article}
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\begin{tabular}{|l|}
  \hline
  \begin{varwidth}{\dimexpr\linewidth-2\tabcolsep}
  \strut some text.
  \end{varwidth} \\
  \hline
\end{tabular}

\begin{tabular}{|l|}
  \hline
  \begin{varwidth}{\dimexpr\linewidth-2\tabcolsep}
  \strut Quite a lot more text than in the first table.
  \end{varwidth} \\
  \hline
\end{tabular}

\begin{tabular}{|l|}
  \hline
  \begin{varwidth}{\dimexpr\linewidth-2\tabcolsep}
    \strut Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur non eros in sem blandit 
    scelerisque vitae ac velit. Etiam turpis mi, tincidunt adipiscing tincidunt nec, laoreet quis 
    metus. Nulla ut est eu odio tempus vulputate. Donec blandit tincidunt leo sit amet ultricies. 
    Maecenas commodo nunc in felis pulvinar elementum. In sapien metus, adipiscing dictum sollicitudin 
    ac, interdum et leo. Phasellus suscipit mi sed purus euismod molestie. Class aptent taciti sociosqu 
    ad litora torquent per conubia nostra, per inceptos himenaeos.\strut
  \end{varwidth} \\
  \hline
\end{tabular}
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

The use of \strut is encouraged as minipages tend to strip the appropriate depth/height of the last/first line of the text block.

share|improve this answer
    
Thank your @Werner, this is exactly what I need and this is simple to integrate into my existing work :-) –  Thibault Dory Feb 21 '12 at 9:14
add comment
\documentclass{article}

\newcommand\mybox[1]{%
  \setbox0\hbox{#1}%
  \ifdim\wd0<\textwidth
    \usebox0
  \else
    \noindent\parbox\textwidth{#1}
  \fi}

\def\x{a b c d e f g h i j k l m}
\begin{document}

\parindent0pt

\fbox{\mybox{a b c}}

\fbox{\mybox{\x. \x \x}}

\fbox{\mybox{\x. \x \x. \x \x

\x\x\x\x\x\x\x\x\x\x\x\x\x}}

\end{document}

enter image description here

share|improve this answer
add comment

For a box with a colored background and frame, use \fcolorbox:

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{varwidth}

\newcommand{\mycolorbox}[1]{{%
    \fboxrule 2pt
    \fcolorbox{Navy!80}{DarkKhaki}{%
        \begin{varwidth}{0.8\linewidth}%
                \strut#1%
        \end{varwidth}%
    }}}

\begin{document}
\mycolorbox{Hello, world!}
\end{document}
share|improve this answer
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.