GeneralHow to use >{\begin{LTXexample}}m{5cm}<{\end{LTXexample}} ?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

How to use >{\begin{LTXexample}}m{5cm}<{\end{LTXexample}} ?

Post by yoyoimut »

Hi,

I want to create a table with 2 columns.
The first column will show latex code and the corresponding rendered output. I use
LTXexample to do that.
The second column will show some description about the code.

I can do this scenario successfully if I put the LTXexample environment in each cell of the first column.
Here is the complete minimal code:

Code: Select all

\documentclass{article}

\usepackage{array}

\usepackage{showexpl}
\lstset{
	basicstyle=\ttfamily\scriptsize,
	tabsize=2,
	explpreset={pos=r,rframe={}}
}

\newenvironment{MyTableEnvi}[2]
{
\newcolumntype{C}{m{#1\linewidth}}
\newcolumntype{D}{>{\scriptsize\centering\arraybackslash}m{#2\linewidth}}
\ignorespaces
\begin{tabular}{|C|D|} 
\hline	
}
{
\end{tabular}
}



\begin{document}

\begin{MyTableEnvi}{0.6}{0.2}
%%%% FIRST ROW
\begin{LTXexample}
$y=\sin x$
\end{LTXexample}
&
Sine function\\ \hline
%%%% SECOND ROW 
\begin{LTXexample}
\[
\frac{dy}{dx}=\cos x
\]
\end{LTXexample}
&
Derivative\\ \hline
\end{MyTableEnvi}

\end{document}
However, I think the above method is not so efficient, because I must type more.
My idea is to create a new column type with >{} and <{} that enclose \begin{LTXexample} and \end{LTXexample}, respectively.
Unfortunately, this idea does not work.
Here is the code that I tried, but it did not work.

Code: Select all

\documentclass{article}

\usepackage{array}

\usepackage{showexpl}
\lstset{
	basicstyle=\ttfamily\scriptsize,
	tabsize=2,
	explpreset={pos=r,rframe={}}
}

\newenvironment{MyTableEnvi}[2]
{
\newcolumntype{C}{>{\begin{LTXexample}}m{#1\linewidth}<{\end{LTXexample}}}
\newcolumntype{D}{>{\scriptsize\centering\arraybackslash}m{#2\linewidth}}
\ignorespaces
\begin{tabular}{|C|D|} 
\hline	
}
{
\end{tabular}
}



\begin{document}

\begin{MyTableEnvi}{0.6}{0.2}
%%%% FIRST ROW
$y=\sin x$
&
Sine function\\ \hline
%%%% SECOND ROW 
\[
\frac{dy}{dx}=\cos x
\]
&
Derivative\\ \hline
\end{MyTableEnvi}

\end{document}

Does anyone know how to solve this problem?

Thank you in advance.

Yoyo

Recommended reading 2024:

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

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

Post Reply