1

I'm dealing with a document where I need to write some text within a box. The current code I'm using was kindly suggested by @campa. It ensures the following features:

  1. The box adapts itself to the sentence, in this way when I write a short sentence, the box will not appear with any blank space.

  2. The code allows the user to change the box width (It would be useful being able to control also height) and it doesn't set by default the entire line width. In this way larger sentences can be compressed as you like.

  3. When the text breaks, the next line starts aligned with - or just slightly after - the colon.

Here some example:

enter image description here

And here the current code:

\documentclass{article}

\newcommand{\foo}[3][\linewidth]{%
   \par\noindent
   \sbox0{\fbox{\textbf{#2:}\quad#3}}%
   \ifdim\wd0<#1%
      \usebox0%
   \else
      \sbox0{\textbf{#2:}}%
      \fbox{%
         \copy0
         \quad
         \parbox[t]{\dimexpr#1-1em-\wd0-2\fboxsep-2\fboxrule\relax}{#3}%
      }%
   \fi
}

\begin{document}

Some random text just to show where the margins are.
Some random text just to show where the margins are.
Some random text just to show where the margins are.

\foo{Assumption}{This is a box.}

\foo{Assumption}{This is a longer box.}

\foo{Assumption}{This is a box almost one line long.}

\foo{Assumption}{This is a box more than one line long.}

\foo{Assumption}{This is the box where I would like the text
to be aligned with the colon when it starts a new line.}

\foo[.8\linewidth]{Example}{\raggedright An example with optional parameter
giving the maximal width, here set to \texttt{.8\string\linewidth}.}

\end{document}

Is there a way to make the corners of the box rounded instead of angular? The radius of curvature shouldn't be exaggerated, just good looking.

Bonus option: It would be also great to be able to decide whether or not get a shadowed box.

I know there is a package called tcolorbox. Is it possible to get the whole result with it?

An example of a rounded-corner box I'm looking for:

enter image description here

I thank everyone who can give me a hand!

1 Answer 1

2

enter image description here

I have a very similar box defined in my code that I use to highlight important information (hence the box is called important):

\documentclass{article}
\usepackage{blindtext}  % Used for dummy text
\usepackage{tcolorbox}
\newtcolorbox{important}{center,%
    colframe=blue!95,%
    colback=blue!5,%
    hbox
}
\begin{document}
    \blindtext
    \begin{important}
        This is a box.
    \end{important}
    
    \blindtext
\end{document}

colframe and colback simply set the frame and background colour and hbox makes the box only as wide as the text inside.

5
  • Thanks @Willoughby. Does this command allow for some alignment function within the box itself? Just to accomplish point 2 and 3 of my first request.
    – Catarella
    May 1, 2021 at 21:00
  • Instead of hbox there is a width parameter that you can set to some length. For alignment within the box you should be able to add in what you have in your MWE for the same alignment but with the tcolorbox.
    – Willoughby
    May 1, 2021 at 21:05
  • Could you show me how I can add the alignment part please? I've tried it without success. I would appreciate it a lot! @Willoughby
    – Catarella
    May 2, 2021 at 15:27
  • Setting textwidth is equivalent to using a \parbox. Setting align is equivalent to using a tabular (with @{} borders). Setting textheight and textdepth is equivalent to using the optional arguments for \raisebox. May 2, 2021 at 16:45
  • I would like to know how to: 1) indent the box with tcolorbox when the box is added at the beginning of a new section. 2) how to align -long-text within the box in the same way I did with the command \foo (you can see the images in my first post). It would be great If someone could give me a MWE to better understand. Thanks!
    – Catarella
    May 2, 2021 at 17:12

You must log in to answer this question.

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