2

I need a box with the following properties

  • framed
  • fixed width
  • content text is centered
  • manual line breaks in the content text are possible

The closest I have is:

\documentclass{article}
\usepackage{minibox}
\begin{document}
\minibox[frame,c]{line 1\\longer line 2}
\end{document}

But I don't find a way to fix the width of the box. I want to set it to a fixed value, which is be wider than the automatic width.

2 Answers 2

4

You could create your own box:

\documentclass{article}

\newcommand{\foo}[1]{%
  \fbox{% <- adds the frame
    \parbox{4cm}{% <- fixes the width
      \centering% <- centers the content
      #1%
    }%
  }%
}%

\begin{document}
\foo{line 1\\longer line 2}
\end{document}
2
0

I would recommend you this answer:

Set the width of a fcolorbox

The idea is to create a command:

\usepackage{xcolor}
\usepackage{xparse}
\NewDocumentCommand{\framecolorbox}{oommm}
 {% #1 = width (optional)
  % #2 = inner alignment (optional)
  % #3 = frame color
  % #4 = background color
  % #5 = text
  \IfValueTF{#1}
   {%
    \IfValueTF{#2}
     {\fcolorbox{#3}{#4}{\makebox[#1][#2]{#5}}}
     {\fcolorbox{#3}{#4}{\makebox[#1]{#5}}}%
   }
   {\fcolorbox{#3}{#4}{#5}}%
 }

And then you can use it this way:

\framecolorbox[4cm]{blue}{blue!40}{what do I do here?}

Which would do:

enter image description here

Of course, if you want it to be a normal box without colors, you can just change the arguments

1
  • 1
    Thank you for the answer. But my impression is that I cannot do manual line breaks within the box.
    – azimut
    Jan 28, 2023 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .