TransWikia.com

How to get a rounded-corner box

TeX - LaTeX Asked on June 28, 2021

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]{%
   parnoindent
   sbox0{fbox{textbf{#2:}quad#3}}%
   ifdimwd0<#1%
      usebox0%
   else
      sbox0{textbf{#2:}}%
      fbox{%
         copy0
         quad
         parbox[t]{dimexpr#1-1em-wd0-2fboxsep-2fboxrulerelax}{#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[.8linewidth]{Example}{raggedright An example with optional parameter
giving the maximal width, here set to texttt{.8stringlinewidth}.}

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!

One Answer

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.

Answered by Willoughby on June 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP