GeneralExample Environment, (a la 'lshort')

LaTeX specific issues not fitting into one of the other forums of this category.
calder
Posts: 5
Joined: Tue Feb 03, 2009 12:00 am

Example Environment, (a la 'lshort')

Post by calder »

What would it take to create a minimal Example environment like the one used in lshort? Basically,

Code: Select all

\begin{example}
LaTeX code
\end{example}
Generates the verbatim "LaTeX code" to the left and a box with the compiled version of the code to the right. I've tried looking at 'lshort.sty' but it includes so much other stuff that it's hard to understand what's going on. A bare minimum example that simply generates verbatim as well as interpreted code chunks would be very welcome.

Thanks in advance,
-Calder Coalson

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Example Environment, (a la 'lshort')

Post by localghost »

I guess the showexpl package is that what you need in this case.


Best regards and welcome to the board
Thorsten¹
calder
Posts: 5
Joined: Tue Feb 03, 2009 12:00 am

Re: Example Environment, (a la 'lshort')

Post by calder »

Hrm, thanks for the help, but unfortunately this isn't quite what I'm looking for. The two problems with it may be very minor, but I've spent nearly 4 hours trying to solve them already and failed, so here they are:
1) There is no line-wrapping for the verbatim code, so the line just keeps going and going and going until it goes right off the page.
2) The font's bad and there's no simple way of changing that. Minor issue, but I really like the default verbatim font style. Much more suitable for code.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Example Environment, (a la 'lshort')

Post by gmedina »

Both issues can be easily solved taking into account that you can use all the power of the listings package (package documentation). Take a look at the following simple example:

Code: Select all

\documentclass{article}
\usepackage{showexpl}
\usepackage{amsmath}
\usepackage{xcolor}

\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    breaklines=true,
    backgroundcolor=\color{lightgray},
    breakautoindent=true,
    captionpos=t
}

\begin{document}

\begin{LTXexample}[wide,width=.75,preset=\footnotesize,rframe=single]
\documentclass[a4paper,twoside]{article}
\usepackage{amsmath}

\begin{document}

\begin{equation}\label{eq:barwq}
  \begin{split}
    H_c&=\frac{1}{2n}\sum^n_{l=0}(1)^{l}(n{l})^{p2}
    \sum_{l _1+\dots+ l _p=l}\prod^p_{i=1}\binom{n_i}{l _i}\\ &\quad
    \cdot[(nl)(n_il_i)]^{n_il_i}\cdot\Bigl[(nl)^2\sum^p_{j=1}(n_il_i)^2\Bigr].
  \end{split}
\end{equation}

\end{document}
\end{LTXexample}

\end{document}
A good starting point would be to have a look a the file showexpl-test.tex that comes in the showexpl.zip file.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
calder
Posts: 5
Joined: Tue Feb 03, 2009 12:00 am

Re: Example Environment, (a la 'lshort')

Post by calder »

Thanks a ton gmedina and localghost! One last problem, and then I promise to stop bugging youguys. How can I include this in a macro? Do I need to define a custom .sty file?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Example Environment, (a la 'lshort')

Post by gmedina »

I don't fully understand your question. What exactly do you want to include in a macro? Please try to describe exactly what you expect to achieve.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
calder
Posts: 5
Joined: Tue Feb 03, 2009 12:00 am

Example Environment, (a la 'lshort')

Post by calder »

It's simply a matter of good form, but it would be nicer to have a command like this:

Code: Select all

\newcommand{example}[1]{
\begin{LTXexample}[bunch-o'-options]
  #1
\end{LTXexample}
}
and simply have to write:

Code: Select all

\example{blablabla}
Than to have to write:

Code: Select all

\begin{LTXexample}[bunch-o'-options]
  #1
\end{LTXexample}
every time.

Now, this isn't a big issue, unless I decided to globally change one of the options at a later date. Besides, it makes the code cleaner. If this isn't easily possible, I'll give up and just do it the sloppy way, but if it's within my capabilities to do, I'd feel dumb not to.

Image
http://xkcd.com/292/

Thanks again for all the help and humoring my idiosyncrasies.
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Example Environment, (a la 'lshort')

Post by phi »

This is no problem, the only thing is that you cannot use a command (because the code has to be written into a file without changes, which isn't easy if it was grabbed as a command argument), but it should be possible to use a custom environment:

Code: Select all

\newenvironment{example}{\LTXexample[options]}{\endLTXexample}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Example Environment, (a la 'lshort')

Post by gmedina »

phi wrote:...but it should be possible to use a custom environment:

Code: Select all

\newenvironment{example}{\LTXexample[options]}{\endLTXexample}
No. That will not work.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Example Environment, (a la 'lshort')

Post by phi »

ok, but still no problem:

Code: Select all

\makeatletter
\def\my@expl@ptions{wide,width=.75,preset=\footnotesize,rframe=single}
\lstnewenvironment{example}{%
	\begingroup
	\advance\c@ltxexample\@ne
	\advance\c@lstlisting\@ne
	\edef\x{%
		\noexpand\lstset{\unexpanded\expandafter{\SX@explpreset},\unexpanded\expandafter{\my@expl@ptions}}%
		\endgroup
		\def\noexpand\SX@codefile{\SX@codefile}%
		\def\noexpand\SX@graphicname{\SX@graphicname}%
		\def\noexpand\SX@graphicparam{\SX@graphicparam}%
	}%
	\x
	\xdef\SX@@explpreset{%
		\unexpanded\expandafter{\my@expl@ptions},%
		codefile=\SX@codefile,%
		graphic={[\SX@graphicparam]{\SX@graphicname}}%
	}%
	\setbox\@tempboxa=\hbox\bgroup
	\lst@BeginWriteFile{\SX@codefile}%
}{%
  \lst@EndWriteFile\egroup
  \SX@put@code@result
}
\makeatother
Post Reply