0

This question already has an answer here:

So I'd like to do something along the line:

\mybox{Some content on the first line \\ some content on the second line \\ and this boxes border are fitting the content}

So I want a box around content which actually fits the content and also respects things like line breaks. How can I do that?

marked as duplicate by Skillmon, Bobyandbob, Zarko, leandriis, Troy Mar 4 '18 at 14:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Would the content have a fixedwidth, or would the width be defined through line breaks (i.e. the width would be that of the longest line)? – Bernard Mar 4 '18 at 13:12
  • Defined by the linebreak but after a lot of googling I just found this tex.stackexchange.com/questions/259149/… – xotix Mar 4 '18 at 13:17
2

A simple solution without any additional packages could use \fbox and a tabular environment.

\documentclass[border=2mm]{standalone}

\newcommand\mybox[2][l]{%
  \fbox{\begin{tabular}{@{}#1@{}}#2\end{tabular}}}

\begin{document}
\mybox{this text\\is in\\my box\\and the border\\fits}
\mybox[c]{centered text\\is also possible\\with the optional\\argument}
\end{document}

enter image description here

Fixed width is easily possible using [p{<width>}] as the optional argument. Right-alignment with [r] (since it's a tabular any column definition would do).

  • see @Werner answer on tex.stackexchange.com/questions/259149/… – Zarko Mar 4 '18 at 13:43
  • @Zarko didn't see that, I only took a look at the first answer :) – Skillmon Mar 4 '18 at 13:44
  • and how could I add a heading s.t. I can have several boxes next to each other? ( so the heading doesn't distort the floating of the boxes?) – xotix Mar 8 '18 at 11:18
  • @xotix That is nothing you requested in your initial question. Take a look at the tcolorbox package for fancy boxes with headings and stuff. – Skillmon Mar 8 '18 at 11:51
1

Here are two ways: the first uses the eqparbox package and an \fbox (or, in the following code \fcolorbox in case you want some colour), the second, a simple tabular with a single centred column, and the makecell package, which allows for line breaks in standard cells.

You also might be interested in the pbox package: the eponymous command is like a \parbox in which the width argument definas actually a minimal width of the box.

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[svgnames, table]{xcolor}
\colorlet{framecolor}{Gold}

\usepackage{eqparbox}
\usepackage{makecell}
\usepackage{bigstrut}
\newcommand{\myfbox}[1]{\arrayrulecolor{framecolor}\begin{tabular}{|@{\,\,}c@{\,\,}|}
\hline
\makecell[l]{\bigstrut[t]#1 \bigstrut[b]}\\
\hline
\end{tabular}}

\begin{document}

\fboxrule = 1pt 
\begin{center}
\fcolorbox{IndianRed}{white}{\eqparbox{mybox}{Some content on the first line \\ some content on the second line \\ and this boxes border are fitting the content.}}
\vskip 1cm

\arrayrulewidth = 1pt
\myfbox{Some content on the first line \\ some content on the second line \\ and this boxes border are fitting the content.}
\end{center}

\end{document}

enter image description here

  • and how could I make a nice heading? If I have two boxes next to each other (like there's no empty line in the latex file between them) they should align nicely. SO like if there was another box around the main box and its title. – xotix Mar 8 '18 at 11:16
  • @xotix: What you'd to have is not clear to me. You want to put a common heading to two boxes sharing a common horizontal axis? – Bernard Mar 8 '18 at 12:17
  • No, just a box and a heading. I solved it by using a colorbox around everything now – xotix Mar 8 '18 at 12:24

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