3

I would like to write a LaTeX document in which several Mathjax commands are shown as source code and compiled.

So I want to have a list of items and have them show up on, for example, the left side as source code for Mathjax e.g. a^2 and on the right side have it show what it will look like e.g. a², but I do not want to enter the code twice for maintenance reasons.

I tried to create a \newcommand for this myself, but failed with verbatim package and listings packages (mostly with improper alphabetic constant errors) and I have tried sverb package with demo environment and showexpl package wit LTXexample environment, but failed with both to have it display the code as it would have to be used with Mathjax (i.e. without math mode symbol $). The document will have to be used by very inexperienced Mathjax users so I do not want to confuse them with symbols they will not have to enter.

Any suggestions for me?

What I tried so far

defining a \newcommand

\usepackage{verbatim}
\newcommand{\cl}[1]{$#1$ & \lstinline{#1}} %use within a \tabular environment

In this case LaTeX tells me that I cannot use \verb in a new command.

\usepackage{listings}
\newcommand{\cl}[1]{$#1$ & \verb!#1!} %use within a \tabular environment

My ultimate goal is to list formulas and this works fine with certain things, but as soon as there is a command in there, e.g. \geq I receive a ! Improper alphabetic constant. error. I found out that I have to escape the \ in the listing with another \, such as \\geq, but of course that will not work for the first part of the command...

I used them like this:

\begin{document}
    \begin{tabular}{c|c}
    $F(x) \geq 0$ & \verb!F(x) \geq 0! \\ 
    \hline 
    \cl{A(n+2)=A(n)+A(n+1)} \\
    \hline
    \cl{\geq} \\
    \end{tabular} 
\end{document}

imported environments

I found packages which aim to provide almost what I searched for:

\usepackage{sverb}
\begin{document}
    \demo{}
    $\geq$
\end{document}

This does perfectly what it intends to do, but in my particular case it is very unpractical, that it (of course) always displays the $ symbols. This is meant to be used by someone who does really not know about LaTeX/Mathjax at all and I did not succeed in explaining why these symbols are in the document, but do not need to be entered in Mathjax.

\usepackage{showexpl}
\begin{document}
    \begin{LTXexample}
    $\geq$
    \end{LTXexample}
\end{document}

The result of this (for my document) was pretty much the same as in the above case.

I tried

7
  • 1
    Hi! Welcome to the site :) As it stands this question is quite hard to answer. The model we follow here is Question and Answer and it generally works best if you come to us with specific problems and a complete Minimal Working Example that demonstrates what you've done and reproduces the problems you are having. – Au101 Feb 10 '16 at 23:33
  • 1
    Store your example code via filecontents package and use it via \lstinputlisting (from listings package) to show the code or via \input to show the result. – Paul Gaborit Feb 11 '16 at 0:49
  • @Au101 Hi, thanks for the help. I wanted to include a minimal example, but I struggled with it because there could be several ways this might work and I really don't care whether I use a command or an environment or something that I just don't know yet. I will now try to include the code I already tried and hope that will clear it up. – anonymous Feb 11 '16 at 8:58
  • @PaulGaborit I've seen that suggested before, but I could not figure out how to execute math environment commands with \input without having the $ symbols show up in the listing. – anonymous Feb 11 '16 at 8:59
  • @Fran I looked at the answers in hat thread, but with those solutions I had the same problems as with the ones I described in my edit above. – anonymous Feb 11 '16 at 9:28
1

I found what I needed in Print small TeX code verbatim and render it.

I used:

\newcommand\showcase[1]{{\ttfamily\detokenize{#1}} & $#1$ }

\begin{document}
    \begin{tabular}{c|c}
    \showcase{\geq}
    \end{tabular} 
\end{document}

Not the answer you're looking for? Browse other questions tagged or ask your own question.