5

How do I typeset "x=A\b" in mcode, which represents "matrix division" in MATLAB? Here is a MWE:

\documentclass[12pt]{report}
\usepackage{amsthm,amsmath, amssymb, paralist, fancybox, listings, mathtools, verbatim, textcomp}
\usepackage{mcode}
\usepackage[unicode,pdfborder={0 0 0 0}, colorlinks=true, urlcolor=red, linkcolor=black]{hyperref}
\usepackage{float}
\usepackage[font=small,labelsep=none]{caption}
\lstset{language=Matlab}
\begin{document}
In MATLAB, it can be solved by executing the statement \mcode{x  =  A \ b}
\end{document}
DJJerome
  • 4,056
  • Not sure how to answer this. First, you'll need the mcode.sty package. Do you have that? Second, what kind of result are you expecting from \mcode{x = A \ b}. It doesn't appear that mcode.sty is a standard package. But I did find a copy posted at this site – A.Ellett Jul 13 '13 at 01:19
  • @A.Ellett: Actually, mcode is available from MathWorks. – Werner Jul 13 '13 at 01:26
  • Yes, I have mcode.sty. I use it quite a bit. \mcode{command} allows one to have MATLAB syntax incorporated throughout ones LaTeX document quite easily. One way of solving a linear system, Ax=b in MATLAB, is through its \ operator. So, you would enter the command x=A\b to obtain the solution x. What I need help with is being able to type "" and have it appear as it would in MATLAB. – DJJerome Jul 13 '13 at 03:03
  • If you double \ as \, then you get the desired output. It seems that something is getting parsed incorrectly as it's being passed from the definition of \mcode to the \lstinline command which is actually doing the formatting. – A.Ellett Jul 13 '13 at 03:54

2 Answers2

3

Consider using the matlab-prettifier package instead:

enter image description here

\documentclass[a4paper]{article}

\usepackage{matlab-prettifier}

% Define a one-character shorthand (") for inline code.
\lstMakeShortInline[style=Matlab-editor]"

\begin{document}
"x=A\b" represents ``matrix division'' in MATLAB.
\end{document}
jub0bs
  • 58,916
2

You could just write something like

\mcode{x = A \\ b }

It seems that the better option is to redefine \mcode as follows:

\renewcommand{\mcode}{\lstinline[basicstyle=\lstbasicfont]}

and then it seems to work fine.

For me with this redefined \mcode

\mcode{x = A \ b}

gets typeset as you want.

Something in the argument is getting processed before it's getting to the \lstinline command. The renewed definition allows \lstinline to process its arguments a bit more effectively.

Basically, mcode.sty defines the following

\newcommand{\mcode}[1]{\lstinline[basicstyle=\lstbasicfont]|#1|}

If you just try something like

\lstinline[basicstyle=lstbasicfont]|x=A\b|

Then you get the result. listings is doing various magic like \verb. By defining the wrapper command \mcode not all the character codes are being processed soon enough.

See \lstinline!Foo! vs \lstinline{Foo} for how newly defined command handles the difference between

\mcode|x=A \ b|

and

 \mcode{x = A \ b}
A.Ellett
  • 50,533
  • Your solutions work for me--both redefining \mcode as well as "doubling up" the \. Thanks for the thorough explanation! – DJJerome Jul 13 '13 at 04:17