30

Is it possible to simply draw a frame around a box which has rounded corners, and be able to control frame width, frame color, frame radius, box background colour (preferably without having to use the complex TikZ package)

eg. (this produces a framed box, but without the required rounded corners)

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\usepackage{fancybox}
\begin{document}

\fboxsep=3pt
\fboxrule=2pt
\def\bordercolor{red}
\def\backgroundcolor{white}
\cornersize{0.9}
\fcolorbox{\bordercolor}{\backgroundcolor} {Here is some text}%

\end{document}
  • 2
    TikZ may be complex but a color box is very simple. Have, e.g., a look at Example: Boxes with text and math. – Schweinebacke Nov 8 '11 at 11:27
  • It is reasonable simple to create a colored frame around a box with a colored background and control the parameters, but the rounded corners makes everything much more complicated, especially the background. For this you need TikZ or a similar package. – Martin Scharrer Nov 8 '11 at 11:48
  • @Schweinebacke thanks, TikZ looks amazing but seems so complicated. Can you recommend a good tutorial on getting started with TikZ? – Dan Nov 8 '11 at 12:16
  • @Dan: What's a good tutorial? IMHO this depends not only on the tutorial but also on the student. I like to read examples and usually use the package manuals to modify them. And I like the examples at the pgf manual. BTW: There are many more TikZ examples at texexample.net. – Schweinebacke Nov 8 '11 at 12:32
  • @Schweinebacke: thanks, it seems the TikZ manual contains a tutorial media.texample.net/pgf/builds/pgfmanualCVS2010-09-28.pdf – Dan Nov 8 '11 at 13:02
26

Here's a method that doesn't require loading TikZ explicitly. (Although mdframed uses tikz behind the scenes. This is, however, the package for framing boxes.

\documentclass{article}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{lipsum}
\begin{document}
\begin{mdframed}[roundcorner=10pt]
  \lipsum[1]
\end{mdframed}
\end{document}

If you don't even want to use TikZ as a backend, you could use PSTricks:

\documentclass{article}
\usepackage[framemethod=PStricks]{mdframed}
\usepackage{lipsum}
\begin{document}
\begin{mdframed}[roundcorner=10pt]
  \lipsum[1]
\end{mdframed}
\end{document}

[Note this doesn't seem to be working for me at the moment.]

  • I see there is a bug. – Marco Daniel Nov 8 '11 at 11:31
  • At the moment I recommend framemethod tikz. – Marco Daniel Nov 8 '11 at 11:36
  • @Seamus Looks like I have an old version of mdframed, as your example does not work for me. I'll update the package, and retry, thanks. – Dan Nov 8 '11 at 12:19
  • Still have a problem after updating mdframed package to version: 2011-10-10 14:44:36... ! Package keyval Error: framemethod undefined. – Dan Nov 8 '11 at 12:55
  • @Dan use \usepackage[style=1]{mdframed} instead. This is the old way of specifying using tikz. – Seamus Nov 8 '11 at 13:09
10

Although when seamus wrote his answer, tcolorbox was not in CTAN, it was announced just one month later. And as nobody has written an answer mentioning it, I do it.

So here you have a little code which shows how to declare a framed box with tcolorbox. How to change it's width, frame color, background color, text alignment, space between text and frame border and rounded corners diameter.

For more funny options you can consult tcolorbox documentation or dive here

\documentclass{article}
\usepackage{tcolorbox}
\begin{document}

\begin{tcolorbox}
Here is some text
\end{tcolorbox}

\begin{tcolorbox}[width=5cm]
Here is some text
\end{tcolorbox}

\begin{tcolorbox}[width=.5\textwidth, colframe=red]
Here is some text
\end{tcolorbox}

\begin{tcolorbox}[width=8cm, colframe=red, colback=blue!30, halign=right]
Here is some text
\end{tcolorbox}

\begin{tcolorbox}[width=.5\linewidth, halign=center, colframe=red, colback=blue!30, boxsep=5mm, arc=3mm]
Here is some text
\end{tcolorbox}

\begin{tcolorbox}[width=7cm, colframe=red, colback=blue!30, arc=3mm, sharp corners=east]
Here is some text
\end{tcolorbox}

\end{document}

enter image description here

4

This another option:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
    \begin{tikzpicture}[x=0.5mm,y=0.5mm]
    \coordinate (a1);
    \coordinate[right=150 of a1](a2);
    \coordinate[below=90 of a1](a3);
    \coordinate[right=150 of a3](a4);
    %% 
    \draw[ultra thick,rounded corners=10] ($(a4)-(50,20)$) rectangle +(50*2,20*2);%Rectangle rounded
    \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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