30

I sometimes want to give examples in a document I am writing. Because these examples are quite general it could be difficult for readers to see the difference between the examples and the theoretical analysis. Therefore I want to put a long vertical bar (that spans the entire example) in the margin indicating the text next to it is an example. It could be that the example uses two pages or more.

Is there any helpful package to do this. An environment would be fine. Something like this:

\begin{example}
...
\end{example}

Other suggestions to indicate the difference are welcome too. First I thought using a different background color. But I want to keep the document printable in Black/White and reading text with a gray background isn't that easy. A horizontal line at the begin and end is also an option but after two pages people could get confused.

6 Answers 6

24

The framed package provides a leftbar environment that does something like what you want:

enter image description here

\documentclass{article}
\usepackage{framed,lipsum}
\begin{document}
\lipsum[1]
\begin{leftbar}
This is a test.
\end{leftbar}
\lipsum[2]
\end{document}
1
  • 6
    Could you edit this answer to include a little explanation. Ideally, answers shouldn't just be a code snippet. All you need to do is say something like "The framed package provides a leftbar environment that does something like what you want"
    – Seamus
    Feb 8, 2011 at 16:26
15

You can also use PDF annotations with the pdfcomment package

\documentclass{scrartcl}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage[svgnames,rgb]{xcolor}
\usepackage[subject={Top1},author={Josef}]{pdfcomment}
\begin{document}
\begin{pdfsidelinecomment}[color=red,icolor=yellow,caption=inline,linebegin={/Butt},lineend={/Square},linewidth=2bp,linesep=1cm]{Example}
\lipsum[2]
\end{pdfsidelinecomment}
\end{document} 

enter image description here

3
  • 1
    Will these work in all PDF readers and will the appear when the document is printed?
    – doncherry
    Jan 15, 2012 at 13:31
  • @doncherry 1. They will work in every PDF reader, which fully implements the PDF spec ;-) No, that's not just Adobe Reader, although that might be the Linux perspective. 2. For printing you have to allow printing annotations in the Prefernces (AR) and in the print dialog. There's a section in the documentation of pdfcomment
    – Josef
    Jan 15, 2012 at 13:44
  • The link of the pdfcomment package is dead. Please consider updating it.
    – evaristegd
    Oct 15, 2018 at 17:03
15

Have a look at changebar package: https://ctan.org/pkg/changebar

An example:

\documentclass{article}
\usepackage[color]{changebar}
\cbcolor{blue}
\usepackage{lipsum}% for random text only
\begin{document}

\cbstart
\lipsum[1]
\cbend

\lipsum[2]
\end{document}
5

mdframed provides a flexible way of adding a (partial) frame around a block of content:

enter image description here

\documentclass{article}
\usepackage{mdframed,lipsum}
\newmdenv[
  leftmargin = 0pt,
  innerleftmargin = 1em,
  innertopmargin = 0pt,
  innerbottommargin = 0pt,
  innerrightmargin = 0pt,
  rightmargin = 0pt,
  linewidth = 3pt,
  topline = false,
  rightline = false,
  bottomline = false
  ]{leftbar}
\begin{document}

\lipsum[1]

\begin{leftbar}
\lipsum[2]
\end{leftbar}

\lipsum[3]

\end{document}

The above output is similar to what is achieved using the leftbar package.

4

Another possibility for indicating an example: If your document has anything to do with code, you might want to consider use of \verb and the verbatim environment. All it does is place the example in monospaced font, and it's wysiwyg. So you can also type up LaTeX examples as-is. I use the \verb command for short, in-line examples, and the verbatim environment for longer ones like this:

\documentclass{article}
\begin{document}

This is the explanation for the \verb#lambda# function:
\begin{verbatim}
>>> #this is a comment 
...
>>> sq = lambda x: x**2
>>> sq(2)
\end{verbatim}
will return \verb#4#.

\end{document}

produces this:

enter image description here

Note you don't have to use ## to delimit the \verb command, you can use another pair, like !!.

3
  • Is this an answer to another question? I fail to see the relation to the OP. Feb 8, 2011 at 18:12
  • @Hendrik: He also asked for other suggestions to indicate examples. If his examples are of code, that could be one way to do it, no? Feb 8, 2011 at 18:21
  • I'd explain that your suggestion is to use a different font "to indicate the difference". After all, this is the part of the question you're addressing, and I honestly didn't get it. Feb 8, 2011 at 19:13
3

Another option comes from the package thmbox, which provide a leftbar environment.

\documentclass{article}
\usepackage{thmbox}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{leftbar}
  \lipsum[2]
\end{leftbar}
\lipsum[3]
\end{document}

enter image description here

You must log in to answer this question.

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