How can I include MATLAB code in Latex without encountering errors?

  • Context: LaTeX 
  • Thread starter Thread starter aaaa202
  • Start date Start date
  • Tags Tags
    Code Latex Source
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
16 replies · 4K views
aaaa202
Messages
1,144
Reaction score
2
I need some kind of environment in Latex where you can write whatever you want without it interpreting it. The reason is I need to include a MATLAB code. Most places I go says I have to use the lstlisting environment, but that doesn't work. Or at least I get this error:
! Package inputenc Error: Unicode char \u8:�\expandafter not set up for use wit
h LaTeX.

See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
 
on Phys.org
aaaa202 said:
I need some kind of environment in Latex where you can write whatever you want without it interpreting it. The reason is I need to include a MATLAB code. Most places I go says I have to use the lstlisting environment, but that doesn't work. Or at least I get this error:
! Package inputenc Error: Unicode char \u8:�\expandafter not set up for use wit
h LaTeX.

See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...

Code:
Can't you put 
      your source code 
      inside code 
tags?
 
maybe. How do you make code tags?
 
aaaa202 said:
maybe. How do you make code tags?

Go back to my post and hit the "quote" button :smile:

There's a button on the edit panel header, too; select the text to be wrapped in code tags and hit the icon.

attachment.php?attachmentid=54664&stc=1&d=1358024302.gif


It's probably just as easy to type in the tags by hand.
 

Attachments

  • Fig2.gif
    Fig2.gif
    9 KB · Views: 1,114
okay but this is for the messages on this site. Does it work in a latex document too? edit: ill just try ofc
 
aaaa202 said:
okay but this is for the messages on this site. Does it work in a latex document too? edit: ill just try ofc

No, it's for posts on this site. It wasn't clear to me that you wanted to create "free-form" text in a general LaTex environment. Sorry about that.

A quick google turned up the listings package which might be of interest.
 
Last edited:
hmm yeh but as said I tried that one and I get the weird error:
! Package inputenc Error: Unicode char \u8:�\expandafter not set up for use wit
h LaTeX.

See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
 
okay I tried lstlset. Now I get error: "Package keyval error"

edit: I am now using listingsutf8 with \lstset
 
aaaa202 said:
hmm yeh but as said I tried that one and I get the weird error:
! Package inputenc Error: Unicode char \u8:�\expandafter not set up for use wit
h LaTeX.

See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
In the opening post you said you tried lstlistings. That package does not work with non-ASCII characters. That's what that cryptic message "Unicode char \u8:�" is about.
 
sorry that comment was written before I tried your solution. Look my previous comment :)
 
well i just used listingsutf 8 and made a \lstset environment. Thought that did the trick, ill read the documentation. Just thought u might recognize the error.
 
reading on the documentation didnt really help. I attached my error if you wonna try help me further :)
[7 <./E1E0vsV0.png (PNG copy)> <./nbundnevsV0.png (PNG copy)>]
<egentilstandedobbelt.png, id=46, 422.32782pt x 316.18124pt>
<use egentilstandedobbelt.png> [8]
<energiegen.png, id=52, 422.32782pt x 316.18124pt> <use energiegen.png>
[9 <./egentilstandedobbelt.png (PNG copy)>]
<hist.png, id=56, 422.32782pt x 316.18124pt> <use hist.png> [10 <./energiegen.p
ng (PNG copy)>] <egentilstandeperiodisk.png, id=60, 422.32782pt x 316.18124pt>
<use egentilstandeperiodisk.png>

! Package keyval Error: c undefined.

See the keyval package documentation for explanation.
Type H <return> for immediate help.
 
The cheap way to do it, is the verbatim environment, but it will not have line numbers or syntax highlighting etc.

Code:
\begin{verbatim}
#include <iostream>
 
using namespace std;
 
int main(void)
{
    cout << "hello world\n";
    cin.get();
 
    return 0;
}
\end{verbatim}