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
Click For Summary

Discussion Overview

The discussion revolves around how to include MATLAB code in LaTeX documents without encountering errors, particularly focusing on the use of the lstlisting environment and issues related to character encoding. Participants explore various methods and potential solutions for formatting code in LaTeX.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants suggest using the lstlisting environment for including MATLAB code, but others report encountering errors related to Unicode characters.
  • One participant mentions that lstlisting only supports ASCII characters, indicating that non-ASCII characters in the code may cause issues.
  • Another participant proposes using the listingsutf8 package and setting the encoding with \lstset as a potential solution.
  • There is a suggestion to use the verbatim environment as a simpler alternative, though it may lack features like line numbers and syntax highlighting.
  • Participants express confusion about how to properly implement \lstset and request clarification on its usage.
  • Errors related to keyval and inputenc packages are discussed, with participants sharing their experiences and troubleshooting attempts.

Areas of Agreement / Disagreement

There is no consensus on the best method to include MATLAB code in LaTeX, as participants present multiple approaches and express varying degrees of success and confusion regarding errors encountered.

Contextual Notes

Participants mention specific error messages related to Unicode characters and keyval issues, indicating potential limitations in the packages being used. The discussion reflects uncertainty regarding the compatibility of different environments with non-ASCII characters.

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.
...
 
Physics news 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,102
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:
It was pretty clear to me!

aaaa202, the problem is that lstlistings only supports ASCII. Your code apparently contains non-ASCII characters. You need to use either the package listingsutf8 or listings and specify the encoding via \lstset.
 
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
 
  • #10
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.
 
  • #11
sorry that comment was written before I tried your solution. Look my previous comment :)
 
  • #12
My mind reading skills aren't up to snuff today. How did you use \lstset? Did you read the documentation on it?
 
  • #13
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.
 
  • #14
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.
 
  • #15
You aren't showing the code. How did you use \lstset?
 
  • #16
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}
 
  • #17
I don't think verbatim can handle non-ASCII characters, either.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
12K
Replies
4
Views
42K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K