15

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.

Cragfelt
  • 4,005
feanor22
  • 321

3 Answers3

16

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

  • 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 05 '14 at 05:24
  • @Mico Yes, there is some problem. Unfortunately I can't debug it now. Hence I retracted the code. Thanks for testing :) –  Aug 05 '14 at 06:12
6

Maybe this can meet your spectations

\documentclass{article}
\usepackage{xcolor}

\newcommand{\gfcb}[1]{%
    \fcolorbox{white}{gray!10!}{\quad\strut #1\quad}
    } % gfcb := gray fcolorbox
\newcommand{\cop}[1]{%
    \gfcb{\texttt{\detokenize{#1}}} 
    \ensuremath{\quad\longrightarrow\quad #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.

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

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}

enter image description here

Cragfelt
  • 4,005
1
  \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.