8

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

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

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}
  • (+1): Thanks @Peter for your answer. This is fantastic. Could you guide me how to include code from external file. Thanks – MYaseen208 Aug 17 '14 at 18:22
  • @MYaseen208: Have updated solution to show how to include file from an external file. – Peter Grill Aug 17 '14 at 19:30
  • This is what I was looking for. Thanks a lot. Last thing, how to get code on the right and output on the left. – MYaseen208 Aug 17 '14 at 19:34
  • Got it. Thanks a lot for your help. Much appreciated. – MYaseen208 Aug 17 '14 at 20:16
  • I encountered two problems. (1)- Chapter, Section environments will be added to the main document, and (2)- not working for beamer. Any thoughts. – MYaseen208 Aug 17 '14 at 21:01
7
+50

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. – user31729 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. – user31729 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 – user31729 Aug 17 '14 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.