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.

While preparing my lecture notes, I decide to create a command, possibly a new environment, that highlights when I am presenting a worked example. I came up with the following code. The code does kinda satisfy my general use and its purpose but I am looking for a possible better code that includes the following:

  1. The environment should use the entire textwidth and expand vertically as text is added. The table kinda does that feature for it expands vertically as you add text. If you notice in my code I forced the textwidth of the table so that it more or else seems to fit the page. I am still working on that.

  2. That the environment possibly expands to a next page by saying where "Example 1" is "Contd. Example 1." I hope this not too hard to figure out.

I forced tikzpicture to be aligned with the table border but I know this is not the best way. Any assistance into the matter will be highly appreciated.

    \documentclass[letterpaper]{article}
    \usepackage{fullpage,color}
    \usepackage{array,tikz}
    \parindent0pt
    \definecolor{mblue}{rgb}{0,0.6,0.8}
    \newcounter{mEgnum}
    \newcommand{\eg}[1]{%
    \par\vspace*{0.5cm}\stepcounter{mEgnum}
    \begin{tabular}{p{15cm}l|}
    \hline\setlength\parindent{-2.17mm}
    \begin{tikzpicture}
    \draw [color=mblue,fill=mblue](0,0) rectangle (0.25,0.75);
    \draw [color=mblue!20,fill=mblue!20](0.25,0) rectangle (3.5,0.75);
    \draw (0.5,0.375) node [right] {\large\textbf{ Example \arabic{mEgnum}}};
    \end{tikzpicture}
    & \tabularnewline
    #1
    &\tabularnewline\cline{2-2}
    \end{tabular}\par\vspace{0.5cm}\noindent%
    }

    \begin{document}
    \eg{Solve the following inequalities and represent the solution set in Set-Builder, Interval Notations and on the Number Line.&\tabularnewline}
    \end{document} 

Sample

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

As it is, your environment will not work if the example is longer than one page. So, to solve your first problem, I would recommend using the mdframed package which will at least handle the case of crossing page boundaries.

enter image description here

Part 2 of your question is a bit harder to do if you want assuming that you want this to look something like:

enter image description here

I attempted to use repeatframetitle option available in the latest version, but was not able to get this to work (posted below). It appears that the package authors are aware of this as it is on their todo list:

Improve page breaks.

Further Enhancements (see non-working solution below):

  • Handle continued examples as documented above
  • I had to tweak the tikz code for the continued example image. That should not really be necessary, so that can probably be improved as well.

Code:

\documentclass[letterpaper]{article}
\usepackage{fullpage,color}
\usepackage{tikz}
\usepackage{mdframed}

\usepackage{lipsum}% for dummy text
%\usepackage{showframe}% to see page frame

\parindent0pt
\definecolor{mblue}{rgb}{0,0.6,0.8}
\newcounter{mEgnum}

\newcommand{\eg}[1]{%
  \topskip=0pt% otherwise there is a space above
  \stepcounter{mEgnum}%
  \begin{mdframed}[
      leftline=false,
      innerleftmargin=0pt,
      skipabove=0pt,
      innertopmargin=0pt,
  ]
  %\setlength\parindent{-2.17mm}% only if you want header to be bleed into margin
  \begin{tikzpicture}
    \draw [color=mblue,fill=mblue](0,0) rectangle (0.25,0.75);
    \draw [color=mblue!20,fill=mblue!20](0.25,0) rectangle (3.5,0.75);
    \draw (0.5,0.375) node [right] {\large\textbf{Example \arabic{mEgnum}}};
  \end{tikzpicture}
  \par\noindent
  #1
  \end{mdframed}
}

\begin{document}
   \eg{\lipsum[1-10]}
\end{document} 

Enhanced Solution (but not yet fully working):

Here is my attempt to use the latest version of mdframed to repeat the title at the top, and also adjust this title if it is not the first page. But for some reason, this is not working and I don't know why. There is a remark in the documentation regarding the repeatframetitle option:

The feature is currently not well implemented!!!

so not sure if this the reason why this does not adjust the frame title on the 2nd page

\documentclass[letterpaper]{article}
\usepackage{fullpage,color}
\usepackage{tikz}
\usepackage{mdframed}

\usepackage{lipsum}% for dummy text
%\usepackage{showframe}% to see pageframe

\parindent0pt
\definecolor{mblue}{rgb}{0,0.6,0.8}
\newcounter{mEgnum}

\newlength{\YShift}
\newlength{\InnerSep}
\newcommand*{\MyHeight}{0.75cm}%
\pgfmathsetlength{\YShift}{\MyHeight/2}%
\pgfmathsetlength{\InnerSep}{1.27ex}% should be computed
\newcommand*{\TitlePrefix}{Example}%
\newcommand{\MyFrameTitle}[1]{% Define a macro for this
   \begin{tikzpicture}
     \draw [color=mblue,fill=mblue](0,0) 
            rectangle (0.25,\MyHeight) 
            node [
                rectangle, right,fill=mblue!20, 
                inner sep=\InnerSep, yshift=-\YShift
             ] 
                {\large\textbf{\TitlePrefix #1}};
  \end{tikzpicture}
}
\newcommand{\eg}[1]{%
  \topskip=0pt% otherwise there is a space above
  \stepcounter{mEgnum}%
  \global\renewcommand{\TitlePrefix}{Example}% Initial title
  \begin{mdframed}[
      leftline=false,
      innerleftmargin=0pt,
      skipabove=0pt,
      innertopmargin=0pt,
      frametitle={\MyFrameTitle{\arabic{mEgnum}}},
      repeatframetitle=true,
  ]
  \par\noindent%
  \global\renewcommand{\TitlePrefix}{Contd Example}% Subsequent Titles
  #1
  \end{mdframed}
}

\begin{document}
   \eg{\lipsum[1-10]}
\end{document} 
share|improve this answer
    
@MarcoDaniel: Is the problem with the 2nd version a bug in mdframed? –  Peter Grill Jan 29 '12 at 3:43
    
I appreciate the input. There are definitely things I need to look more into. I will look at what you have shared here and give you my feedback. –  azetina Jan 29 '12 at 19:16
add comment

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.