[Tex/LaTex] How to show latex commands in text mode

math-modeoutputtext-modeverbatim

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.

Best Answer

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}

enter image description here

Related Question