Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Are you specifically looking for solutions with \colorbox? Otherwise, you should be able to do so with mdframed as it has been explained in a question of yours. –  Claudio Fiandrino Mar 6 at 16:02
    
I looking for the solutions except mdframed. In \colorbox is this possible? –  PSK Mar 6 at 16:04
    
How about \hrulefill before and after the text? –  Henri Menke Mar 6 at 16:05

4 Answers 4

up vote 2 down vote accepted

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

share|improve this answer
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 6 at 17:31

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

share|improve this answer
    
thak you it is working. –  PSK Mar 6 at 16:35

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

share|improve this answer
    
thanks..@lhnasi.. –  PSK Mar 6 at 16:35
    
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 6 at 17:04

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

share|improve this answer
    
Is it possible to make it in any other color like green, blue? ..@Steven B. Segletes –  PSK Mar 6 at 16:08
    
Sorry you misunderstanding my question..I asked the color of the rule.@Steven B. Segletes –  PSK Mar 6 at 16:15
    
@PSK Aha. Done. –  Steven B. Segletes Mar 6 at 16:16
    
thank u so much...its working...@steven B. Segletes –  PSK Mar 6 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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