2

I know there have been a number of posts relating to this topic, in particular this one, but I find it impossible to believe that it's simply impossible to do the following incredibly basic thing within latex: I write problem sets that include questions requiring answers that contain matlabe code. I have a macro called answer:

\ifthenelse{\theanswerkey=1}{}{%
     \def\answer#1{\iffalse#1\fi}}

I set the counter answerkey to 1 to print the answer key.

I then want to write something like

\answer{
if 1 > 0 
    do this
else
    do that
end
}

and have the indentations preserved in the latex output. This appears to be completely impossible: \texttt doesn't respect indentation, verbatim doesn't work inside macros, nor does lstlisting. I gather \obeylines doesn't work either.

Do I need to switch back to MS word, or is there some way to force latex to do this incredibly simple thing that it appears to be beyond it's capabilities?

If anybody could help restore my shattered love for latex, I'd be most grateful.

5

The question is very far from being clear. I assume you want to have a conditional that tells LaTeX to print the answer or to ignore it.

It is simple. The important thing is not to have the lstlisting environment in the argument to another command, but in a conditional it is OK.

\documentclass{article}
\usepackage{listings}

\newif\ifanswers

%\answerstrue % uncomment if you want to print answers

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
}

\begin{document}

Write a program.
\ifanswers
\begin{lstlisting}
a+b=c
d-e=f
\end{lstlisting}
\fi

%%% I set \answerstrue just to show it works

\answerstrue

Write a program.
\ifanswers
\begin{lstlisting}
a+b=c
d-e=f
\end{lstlisting}
\fi

\end{document}

enter image description here

  • I'm almost sure, that this is not valid MATLAB code ;) – Henri Menke Mar 16 '16 at 12:21
  • 1
    You might also want to point out, that there is matlab-prettifier, an extension for listings to get better highlighting for MATLAB code. – Henri Menke Mar 16 '16 at 12:23
  • Works great, thanks. And indeed that was not matlab code! Wish there were a bit more documentation for matlab-prettifier for those of us who aren't smart enough to run makefiles successfully. – Leo Simon Mar 16 '16 at 18:17
  • @HenriMenke I have the same impression. :) – egreg Mar 16 '16 at 18:28
  • It's worth noting that the matlab-prettifier manual suggests that you might want to switch from the default Computer Modern typewriter font to Beta Mono. As I just discovered, however, you cannot cut and paste from a document compiled with this font to the matlab command line. Since this for me was the whole purpose of the exercise, I'm back to the default Computer modern font, which, while ugly, has the huge advantage that it can be cut and pasted. – Leo Simon Apr 18 '16 at 8:14

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.