# How to show latex commands in text mode

Sorry for the basic question. What I want to do is to show the LaTeX command and, right after that, the output. But how can I show LaTeX formulas without obtaining errors for not using the math mode environment.

The problem is if I put the $...$, the formula appears and not the command. If I dont use the $...$ I get an error.

I have tried also to put inside a \text{}. No good either. Any suggestions? Thanks in advance.

• Have you tried the verbatim package? Aug 5 '14 at 3:52
• You can try \verb!<code for formula>!. If you want syntax highlighting then use listings package.
– user11232
Aug 5 '14 at 3:52
• Use \verb+latex_code+. Aug 5 '14 at 3:53
• I guess tex.stackexchange.com/questions/110349/… and tex.stackexchange.com/questions/19295 may be of interest. Aug 5 '14 at 5:08
• The proper solution to this is the showexpl package as per the links of @TorbjørnT's comment. That way you don't have to duplicate the code. Aug 5 '14 at 5:26

You can use \verb as

\verb!$x^2 + y^2 = r^2$!


Or better use listings package

\lstinline[language={[LaTeX]TeX},basicstyle=\ttfamily]{$\sin^2\theta+\cos^2\theta=1$}


Full code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{listings}

\begin{document}
Using \verb|\verb|:

\verb!$x^2 + y^2 = r^2$! \quad $\longrightarrow$ \quad $x^2 + y^2 = r^2$

Using \verb|listings|:

\lstinline[language={[LaTeX]TeX},basicstyle=\ttfamily]{$x^2 + y^2 = r^2$} \quad $\longrightarrow$ \quad $x^2 + y^2 = r^2$
\end{document}


• Your \latexcode macro is ingenious. Unfortunately, it doesn't seem to be robust to the inclusion of instructions such as \sin in its argument. E.g., the instruction \latexcode{$\sin^2\theta+\cos^2\theta=1$} produces the error message ! Improper alphabetic constant. (No such error occurs when using \verb|...|.) Can your code by generalized to include this example application?
– Mico
Aug 5 '14 at 5:24
• @Mico Yes, there is some problem. Unfortunately I can't debug it now. Hence I retracted the code. Thanks for testing :)
– user11232
Aug 5 '14 at 6:12

Maybe this can meet your spectations

\documentclass{article}
\usepackage{xcolor}

\newcommand{\gfcb}[1]{%
} % gfcb := gray fcolorbox
\newcommand{\cop}[1]{%
\gfcb{\texttt{\detokenize{#1}}}
} % cop := code output

\begin{document}

% Examples of usage

\cop{\ln x} \\

\cop{\sin x} \\

\cop{4\cdot a^n} \\

\cop{\sqrt{x^2+y^2}} \\

\cop{\displaystyle\int\cot^2 x\,dx} \\

\cop{\overline x=\dfrac{\sum_{i=1}^n x_i}{n}} \\

\cop{\displaystyle\lim_{\lVert \Delta x\rVert\to0}\sum_{k=1}^{n}f(c_i)\Delta x_i} \\

\cmdc{f(x)=\begin{cases}
x-4 & \text{if } x\leq 4 \\
x^3 & \text{if } x>4
\end{cases}} \\

\end{document}


I casually made it a couple of months ago when I had the same question while I was wondering how to display a command and its output for a LaTeX potential manual. And surfing TeX.SE I have just met this question.

The issue I got is that the code input can only be displayed in one line, so for big equations, that would not look good and would not fit the width of the page or paragraph.

UPDATE

In case you need to show large equations, you can use a tcolorbox that displays a code listing and its output.

\documentclass{report}
\usepackage{amsmath,amsthm,amssymb,latexsym}
\usepackage{tcolorbox.doc.s_main}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\definecolor{midnightBlue}{RGB}{25,25,112}
\definecolor{webblue}{rgb}{0, 0, 0.5}

\begin{document}

\DeclareTCBListing{mybox}{ s O{} m }{%
colback=blue!10!,
colframe=webblue,
bicolor,
colframe=midnightBlue,
colbacklower=white,
after title={\hfill Output},
fonttitle=\bfseries,
IfBooleanTF={#1}
{text side listing}{listing side text},
title=#3,#2}

% Tcolobox with title
\begin{mybox}{Code}
$f(x)=\begin{cases} x-4 & \text{if } x\leq 4 \\ x^3 & \text{if } x>4 \end{cases}$
\end{mybox}

% Tcolorbox without title
\begin{mybox}{}
$P(k):2(k+1)-3\leq2(k+1)-2$
\end{mybox}

\end{document}


  \begin{document}
\begin{tabular}{cll}
\begin{minipage}{3cm}
\begin{verbatim}
\ln x
\end{verbatim} \end{minipage} & $\longrightarrow$ &
lnx\\
\begin{minipage}{2cm}
\begin{verbatim}
4 \cdot a^n
\end{verbatim} \end{minipage} & $\longrightarrow$ &
$4 \cdot a^{n}$\\
\begin{minipage}{2cm}
\begin{verbatim}
\sqrt{x^2+y^2}
\end{verbatim} \end{minipage} & $\longrightarrow$ &
$\sqrt{x^2+y^2}$\\
\end{tabular}

\end{document}


This has no packages. You have a lot of freedom to modify it. I am sure that you will enjoy it.