9

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

MYaseen208
  • 8,587
  • 1
    tcolorbox and its tcblisting environment, however not with begin{document in it –  Aug 15 '14 at 12:33

2 Answers2

9

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}

Peter Grill
  • 223,288
8

The tcolorbox provides means for nice typesetting and direct output of listings LaTeX code. There are many conceptual and visual configuration possibilities, see the documentation of tcolorbox, e.g. in chapter 9 of the current version 3.12.

However, a \begin{document}....\end{document} cannot be catched easily. Just relaxing \begin{document} etc. is no option. The document environment has to be redefined within a group, such that it's capsuled and does not influence the real outer document environment. However, this will drop some content defined within a potential \AtBeginDocument or \AtEndDocument.

In principle any command allowed only in Preamble will break this example.


\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%
\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}
}%



\tcbset{listing engine={listings}}
\begin{document}

% Redefine the document environment within a group
\begingroup
\renewenvironment{document}{%
  }{%
 }%

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

\endgroup

\end{document}

enter image description here

  • Nice solution. Wonder how to put code and output into two different boxes. – MYaseen208 Aug 15 '14 at 14:27
  • @MYaseen208: Do you wonder or did you wonder? The tcblisting boxes can be configured individually to contain listing and output, only listing or only output... The settings of listings package (your \lstset are used, you can change it of course. –  Aug 15 '14 at 14:29
  • @MYaseen208: Why did you not be more specific about the two boxes? If I would have had more information on that, I could have updated my answer. –  Aug 17 '14 at 20:12
  • Never mind. I found tcolorbox more appealing but have issues with it. I could not figure out how to put code and output into two boxes and was not able to read code from external file (lack of knowledge). Would appreciate if could help me. Thanks – MYaseen208 Aug 17 '14 at 21:06
  • @MYaseen208: I'll try tomorrow –  Aug 17 '14 at 21:32
  • Thanks. Looking forward to your answer and help. Much appreciated. – MYaseen208 Aug 17 '14 at 21:35