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.


3 Answers 3


You can use \verb as

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

Or better use listings package


Full code:


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$

enter image description here

  • 1
    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, 2014 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, 2014 at 6:12

Maybe this can meet your spectations


    \fcolorbox{white}{gray!10!}{\quad\strut #1\quad}
    } % gfcb := gray fcolorbox
    \ensuremath{\quad\longrightarrow\quad #1}
    } % cop := code output


% 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} \\

x-4 & \text{if } x\leq 4 \\
x^3 & \text{if } x>4
\end{cases}} \\


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.

enter image description here

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.

enter image description here


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


\definecolor{webblue}{rgb}{0, 0, 0.5}


\DeclareTCBListing{mybox}{ s O{} m }{%
after title={\hfill Output},
{text side listing}{listing side text},

% Tcolobox with title
x-4 & \text{if } x\leq 4 \\
x^3 & \text{if } x>4

% Tcolorbox without title


enter image description here

      \ln x
   \end{verbatim} \end{minipage} & $\longrightarrow$ & 
 4 \cdot a^n
  \end{verbatim} \end{minipage} & $\longrightarrow$ &
  $4 \cdot a^{n}$\\
 \end{verbatim} \end{minipage} & $\longrightarrow$ & 


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

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.