[Tex/LaTex] Using listings with output

listings

I wonder how to get the output of listings with code. Here is my MWE with output. Now I want to output of this listings too. Any help will be highly appreciated. Thanks


Code


\documentclass{article}
\usepackage{listings}
\usepackage{color}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}

\begin{document}

\begin{lstlisting}
\begin{document}
Welcome to \LaTeX.
\end{document}
\end{lstlisting}

\end{document}

Output


enter image description here

Best Answer

The best way to show the output along with the LaTeX code is to use the showexpl package:

enter image description here

Notes:

  • To adjust the position of the output you can use the pos= option: top, bottom, right, left, outer, and inner.

Code:

%\RequirePackage{filecontents}% Comment out so that "example.tex" is not overwritten
\begin{filecontents*}{example.tex}
    Here is some \LaTeX code in an 
    \emph{external} file.  
    The input file is not altered in
    terms of line breaks, but the
    output is properly typeset.
\end{filecontents*}

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

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}

\begin{document}

Use the environment \verb|LTXexample| to show the code and its output:
\medskip
\begin{LTXexample}[width=0.60\linewidth]
\begin{document}
Welcome to \LaTeX.
\end{document}
\end{LTXexample}

\bigskip
To include code from an external file, use \verb|\LTXinputExample|, 
and also applied \verb|pos=r| to have output on right: 
\medskip
\LTXinputExample[width=0.5\linewidth,pos=r]{example.tex}

\end{document}
Related Question