5

I am using the following code to generate a Colored Box in Latex:

\renewenvironment{leftbar}[1][\hsize]%
{%
        \def\FrameCommand%
        {%                  
             \fboxsep=\FrameSep\colorbox{Lavender!25}%
        }%
        \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
}%
{\endMakeFramed}

\leftbar
\textbf<\textbf{Data Type}> <\textbf{Array Name}> \textbf{[RowSize]  [ColumnSize]}; 
\endleftbar

The Settings for the colored box are fixed before: "\begin{document}".

Everytime, I draw a box using "\leftbar" "\endleftbar" -- it is always of the same color I mentioned in the Settings above.

But I want to get Boxes with Different Colors (Rest of the settings same as above).

It seems that "\leftbar" don't take the "color" as an Argument.

Can anybody please guide me on how to generate boxes with Different colors using "\leftbar"?

  • 1
    If you are interested in typesetting code, the listings package could be of interest for you. – Gonzalo Medina Aug 14 '12 at 12:04
6

You can use an argument for the color in the (re)definition of leftbar:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{framed}

\renewenvironment{leftbar}[2][\hsize]%
{%
        \def\FrameCommand%
        {%                  
             \fboxsep=\FrameSep\colorbox{#2}%
        }%
        \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
}%
{\endMakeFramed}

\begin{document}

\begin{leftbar}{green!25}
\textbf<\textbf{Data Type}> <\textbf{Array Name}> \textbf{[RowSize]  [ColumnSize]}; 
\end{leftbar}

\begin{leftbar}{red!25}
\textbf<\textbf{Data Type}> <\textbf{Array Name}> \textbf{[RowSize]  [ColumnSize]}; 
\end{leftbar}

\begin{leftbar}{olive!25}
\textbf<\textbf{Data Type}> <\textbf{Array Name}> \textbf{[RowSize]  [ColumnSize]}; 
\end{leftbar}

\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.