19

Does anyone know how to control the width and color of the left bar in the leftbar environment?

1
  • 2
    Please mention used packages etc. also in the text and not only in the tags. You mean the leftbar environment of the framed package, don't you? Note that this package is kind of superseded by the similar mdframed package. Jul 7, 2011 at 13:53

2 Answers 2

24

Ignore the optional argument:

\documentclass{article}
\usepackage{xcolor}
\usepackage{framed}

\renewenvironment{leftbar}[1][\hsize]
{%
    \def\FrameCommand
    {%
        {\color{red}\vrule width 3pt}%
        \hspace{0pt}%must no space.
        \fboxsep=\FrameSep\colorbox{yellow}%
    }%
    \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
}
{\endMakeFramed}

\def\fact{It is practically a big lie that LaTeX makes you focus on the content without bothering about the layout.}
\begin{document}
\fact
\leftbar
\fact
\endleftbar
\fact
\end{document}

enter image description here

Specify the optional argument:

You can specify the optional argument as the following example:

\documentclass{article}
\usepackage{xcolor}
\usepackage{framed}

\renewenvironment{leftbar}[1][\hsize]
{%
    \def\FrameCommand
    {%
        {\color{red}\vrule width 3pt}%
        \hspace{0pt}%must no space.
        \fboxsep=\FrameSep\colorbox{yellow}%
    }%
    \MakeFramed{\hsize#1\advance\hsize-\width\FrameRestore}%
}
{\endMakeFramed}

\def\fact{It is practically a big lie that LaTeX makes you focus on the content without bothering about the layout.}
\begin{document}
\fact
\leftbar[0.75\linewidth]
\fact
\endleftbar
\fact
\end{document}

enter image description here

2
  • How do I make the box itself colorless (i.e., I want it to have the same color of the background)?
    – user1999
    Jul 7, 2011 at 19:06
  • change that \fboxsep=\FrameSep\colorbox{yellow}% to % \fboxsep=\FrameSep\colorbox{yellow}%
    – user11486
    Feb 5, 2012 at 21:24
13

The width and color (i.e. the non-use of color) seems to be hard-coded into this environment:

\leftbar:
\long macro:->\def \FrameCommand {\vrule width 3pt \hspace {10pt}}\MakeFramed {\advance \hsize -\width \FrameRestore }


\endleftbar:
\long macro:->\endMakeFramed 

Simply define your own version which the appropriate color and width. The best thing is to add length register which values can be changed afterwards:

\documentclass{article}

\usepackage{framed}
\usepackage{xcolor}
\usepackage{lipsum}% dummy text


\newlength{\leftbarwidth}
\setlength{\leftbarwidth}{3pt}
\newlength{\leftbarsep}
\setlength{\leftbarsep}{10pt}

\newcommand*{\leftbarcolorcmd}{\color{leftbarcolor}}% as a command to be more flexible
\colorlet{leftbarcolor}{black}

\renewenvironment{leftbar}{%
    \def\FrameCommand{{\leftbarcolorcmd{\vrule width \leftbarwidth\relax\hspace {\leftbarsep}}}}%
    \MakeFramed {\advance \hsize -\width \FrameRestore }%
}{%
    \endMakeFramed
}


\begin{document}

\begin{leftbar}
\lipsum
\end{leftbar}


\setlength{\leftbarwidth}{5pt}
\setlength{\leftbarsep}{8pt}
\colorlet{leftbarcolor}{blue}

\begin{leftbar}
\lipsum
\end{leftbar}

\end{document}

enter image description here

3
  • Sorry for asking on such an old post. How to get rid of the vertical space before and after the leftbar environment? Apr 12, 2018 at 11:01
  • 1
    @AymaneFihadi: Looking at the manual of the used framed package tells that this space is stored in the length \OuterFrameSep. So use \setlength\OuterFrameSep{0pt} to remove it completely. It might be however, that you need a small amount to avoid the text sticking together (maybe 1ex, \baselineskip, or \smallskipamount, or similar). Test it out. Apr 12, 2018 at 18:51
  • Thank you, it was very kind of you to go through the manual for responding. \setlength\OuterFrameSep{0pt} weirdly get rid of the space before the frame but not the space after the frame. Apr 14, 2018 at 0:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .