3

Is it possible in LaTeX to display inlined LaTeX code? I would like to write a CheatSheet and show side by side Equations and the code inside a tabularx environment:

\documentclass{article}
\begin{document}
%This works
\begin{verbatim}
$x^y\cdot\frac{1}{2}$
\end{verbatim}
%This does not work
\VERB{$x^y\cdot\frac{1}{2}$}
\end{document}
nowox
  • 1,375

3 Answers3

5

you use wrong command name, instead of \VERB{...} should be \verb{...}. For selecting verbatim delimiter you should be careful, it should not be part of text in verbatim.

Your MWE, considering aforemntioned works fine:

\documentclass{article}

\begin{document} This works \begin{verbatim} $x^y\cdot\frac{1}{2}$ \end{verbatim}

And this also work if you use correct command name: \verb+B{$x^y\cdot\frac{1}{2}$+ \end{document}

enter image description here

Zarko
  • 296,517
4

I normally use the package showexpl (which sits on top of listings):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{showexpl}
\begin{document}
    This is a LaTeX example side by side
    \begin{LTXexample}[varwidth=true, basicstyle=\small\ttfamily]
        \[
            \int_a^b c\,dt
        \]
    \end{LTXexample}
    or below
    \begin{LTXexample}[pos=t, basicstyle=\small\ttfamily]
        \[
            \int_a^b c\,dt
        \]
    \end{LTXexample}
\end{document}

enter image description here

It's highly configurable via all the options available for listings.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 3
    There shouldn't be extra space at the top of the boxed displays. See https://tex.stackexchange.com/q/36954 for an explanation and how to fix it. – barbara beeton Aug 27 '22 at 20:14
1

If you don't need the complexity of using showexpl as in Rmano's answer, you can just use listings alone (although for demonstrating examples and code together it's fantastic). I usually combine it with fancyvrb which makes inline code simple to write also. Here's just one possible setup, which I use in documenting my own packages listings, but is highly configurable so you can choose a format that suits you.

\documentclass{article}
\usepackage{listings}
\usepackage{fancyvrb}
\DefineShortVerb{\|}

\lstset{% basicstyle=\ttfamily\small, commentstyle=\itshape\ttfamily\small, showspaces=false, showstringspaces=false, breaklines=true, breakautoindent=true, frame=single captionpos=t language=TeX }

\begin{document}

It's useful to have a simple inline code markup like |\bfseries|.

For larger pieces of code use |{lstlisting}|:

\begin{lstlisting} \documentclass{article} \usepackage{listings} \usepackage{fancyvrb} \DefineShortVerb{|}

\lstset{% basicstyle=\ttfamily\small, commentstyle=\itshape\ttfamily\small, showspaces=false, showstringspaces=false, breaklines=true, breakautoindent=true, frame=single captionpos=t language=TeX } \end{lstlisting} \end{document}

output of code

Alan Munn
  • 218,180