8

I am new to latex i want to add rule in top and bottom of the color box,

My sample code is,

\documentclass{article}
\usepackage{graphicx,xcolor,lipsum}
\begin{document}
\colorbox{pink}{\parbox{\textwidth}{%
 \vskip10pt
 \leftskip10pt\rightskip10pt
 \lipsum[1]
\vskip10pt
}
}
\end{document}
  1. My Expected output is,

enter image description here

Any one help me to achieve this task.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
PSK
  • 1,032

4 Answers4

7

Use mdframed:

\documentclass{article}
\usepackage{mdframed}
\usepackage{xcolor}
\usepackage{lipsum}

\newmdenv[
hidealllines=true,
backgroundcolor=pink,
topline=true,
bottomline=true,
linewidth=3pt,
linecolor=red
]{mybox}

\begin{document}

\begin{mybox}
 \lipsum[1]
\end{mybox}
\end{document}

enter image description here

Gonzalo Medina
  • 505,128
4

You could use \hrulefill before and after your text, which doesn't give exactly the output you sketched, but which is easy to implement.

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{xcolor,lipsum}
\begin{document}
\colorbox{pink}{%
    \parbox{\textwidth}{%
        \hrulefill
        {% We don't want \...skip to affect \hrulefill
            \vskip10pt
            \leftskip10pt\rightskip10pt
            \lipsum[1]
            \vskip10pt
        }
        \hrulefill
    }
}
\end{document}

enter image description here

Henri Menke
  • 109,596
  • 2
    You need \noindent\colorbox{pink}{\parbox{\dimexpr\textwidth-2\fboxrule-2\fboxsep\relax}{%... in order to prevent overfull boxes; you also need to comment out a spurious blank space after the nect to last closing brace. – Gonzalo Medina Mar 06 '14 at 17:31
3

You could stack it. Note that I played with your \parbox width to make the box fall within the proper margins. I also took the liberty of placing it in a separate macro \mycolorbox. Note that it cannot break across page boundaries.

\documentclass{article}
\usepackage{graphicx,xcolor,lipsum}
\usepackage{stackengine}
\parskip 1ex
\newcommand\mycolorbox[1]{%
\par\noindent\stackunder[-3pt]{\stackon[-3pt]{%
\colorbox{pink}{\parbox{\textwidth-10pt}{%
 \vskip10pt
 \leftskip10pt\rightskip10pt
 #1
\vskip10pt
}
}%
}{\color{blue}\hsmash{\rule{\textwidth+10pt}{3pt}}}%
}{\color{blue}\hsmash{\rule{\textwidth+10pt}{3pt}}}
\par}
\begin{document}
\mycolorbox{\lipsum[1]}
\lipsum[1]
\end{document}

enter image description here

If I misunderstood about making the rules extend slightly into the margins, you can make them \textwidth without the +10pt addition (or smaller ). Here is a variation:

\documentclass{article}
\usepackage{graphicx,xcolor,lipsum}
\usepackage{stackengine}
\parskip 1ex
\newcommand\mycolorbox[1]{%
\par\noindent\stackunder[-5pt]{\stackon[-5pt]{%
\colorbox{pink}{\parbox{\textwidth-10pt}{%
 \vskip10pt
 \leftskip10pt\rightskip10pt
 #1
\vskip10pt
}
}%
}{\color{blue!70}\hsmash{\rule{\textwidth-4pt}{2pt}}}%
}{\color{blue!70}\hsmash{\rule{\textwidth-4pt}{2pt}}}
\par}
\begin{document}
\mycolorbox{\lipsum[1]}
\lipsum[1]
\end{document}

enter image description here

3

With tcolorbox

\documentclass{standalone}
\usepackage{lipsum}
\usepackage{tcolorbox}

\newtcolorbox{Mybox}{
  colback=pink!90!black,
  colframe=black,
  arc=0pt, outer arc=0pt,
  boxrule=0pt,
  toprule=3pt,
  bottomrule=3pt
}

\begin{document}

\begin{Mybox}
\lipsum[1]
\end{Mybox}

\end{document}

enter image description here

Previous example is the classic one, but with tcolorbox we can do more interesting boxes.

\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[skins,breakable]{tcolorbox}

\newtcolorbox{Mybox}{
  width={\linewidth-6pt},
  breakable,
  enhanced,
  colback=pink,
  colframe=black,
  arc=0pt, outer arc=0pt,
  boxrule=0pt,
  toprule=0pt,
  bottomrule=0pt,
  enlarge top by=5mm,
  overlay ={
    \draw[gray,thick] ([xshift=5mm]frame.north west)--([yshift=5mm]frame.north)--([xshift=-5mm]frame.north east);
    \draw[fill=gray] ([yshift=4.5mm]frame.north) circle(3pt);
    \draw[line width=3pt, blue!30!black, shorten >=-3pt, shorten < = -3pt] (frame.north west)--(frame.north east);
    \draw[line width=3pt, blue!30!black, shorten >=-3pt, shorten < = -3pt] (frame.south west)--(frame.south east);
  }
}

\newenvironment{Mypage}
{\par\centering\begin{Mybox}}{\end{Mybox}\par}

\begin{document}

\lipsum[2-3]
\begin{Mypage}
\lipsum[1]
\end{Mypage}
\lipsum[3]
\end{document}

enter image description here

Ignasi
  • 136,588
  • Ah, I was adding the tcolorbox option to my answer when you published yours. I'll delete the tcolorbox option from my answer. – Gonzalo Medina Mar 06 '14 at 17:04