3

I am currently learning LaTeX from 'The Not So Short Introduction To LaTeX' and in that book many examples are displayed side by side:

$a^2$ 'Left side of page' --------------------------------- 'right side of page' a^2

I would like to do the same in the paper I am writing, but can not seem to find the command to this?

egreg
  • 1,121,712
fYre
  • 253
  • 1
  • 3
  • 6
  • Use showexpl package. – kiss my armpit Dec 25 '13 at 17:06
  • Hi, welcome to TeX.SX. In addition to the answer already given, there have been some similar questions before, for example: http://tex.stackexchange.com/questions/19295/side-by-side-source-and-output-when-documenting-a-style-file – Torbjørn T. Dec 25 '13 at 17:23

1 Answers1

5

showexpl was written to solve your problem.

\documentclass[preview,border=12pt,12pt]{standalone}% change it back to article

\newlength{\parindentsave}
\AtBeginDocument{\setlength{\parindentsave}{\parindent}}

\usepackage{xcolor}
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\usepackage{showexpl}
\lstset
{
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{white}\noaccsupp,% to hide number lines
    frame=single,
    framesep=\fboxsep,% expands outward, cannot affect if frame=none
    framerule=\fboxrule,% expands outward, cannot affect if frame=none
    rulecolor=\color{red},% cannot affect if frame=none
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    breakindent=0pt,
    tabsize=2,
    columns=flexible,
    language={[LaTeX]TeX},
    basicstyle=\small\ttfamily\hbox{},
    keywordstyle=\color{blue},
    backgroundcolor=\color{cyan!10},
    pos=r,
    width=0.5\linewidth,
    preset=\setlength{\parindent}{\parindentsave},
    explpreset={},
}

\usepackage{tikz}

\begin{document}


\begin{LTXexample}[pos=b,width=\linewidth]
Merry Christmas December 25, 2013 and Happy New Year 2014
\end{LTXexample}


\begin{LTXexample}
Solve $ax^2+bx+c=0$.
\end{LTXexample}

\LTXexample[preset=\centering]
\tikzpicture
\draw (0,0) rectangle (4,3);
\fill[red] (2,1.5) circle (1);
\endtikzpicture
\endLTXexample

\end{document}

enter image description here

  • Just want to highlight three important options to enable side-by-side in \lstset: width=.5\textwidth, breaklines=true, and pos=r (which shows rendered code to the right, or pos=l for left, but NOT pos=b for below). – Quar Jan 02 '20 at 18:18