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

In summary: If that's the case, then you'll need to use one of the package environments that supports them, like listings or UTF8.In summary, the user is trying to include a MATLAB code in a document using LaTeX, but is having problems due to the lack of an environment that can handle the code. The user has tried using the lstlisting environment, but that does not work. He or she has then tried using listings, but that also does not work. The last resort is to use the verbatim environment, which does not work either because it does not support non-ASCII characters. The user has then tried using the keyval package, but that also does not work. He or she has
  • #1
aaaa202
1,169
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
  • #2
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?
 
  • #3
maybe. How do you make code tags?
 
  • #4
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,017
  • #5
okay but this is for the messages on this site. Does it work in a latex document too? edit: ill just try ofc
 
  • #6
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:
  • #7
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.
 
  • #8
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.
...
 
  • #9
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.
 

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

1. What is the purpose of including source code in LaTeX?

Including source code in LaTeX allows for the creation of technical documents and reports that require the insertion of code snippets, such as computer programs, algorithms, or mathematical equations. It also helps in maintaining the formatting and syntax of the code, making it easier to read and understand for both the writer and the reader.

2. How can I include source code in my LaTeX document?

To include source code in your LaTeX document, you can use the verbatim environment or the lstlisting package. Both options provide a way to typeset code without any formatting or syntax highlighting. If you want to add syntax highlighting, you can use the minted package which requires an additional installation of pygments and allows for customizable code formatting.

3. Can I include code from different programming languages in the same document?

Yes, you can include code from different programming languages in the same document using the lstlisting package. It provides language-specific options that allow you to define the programming language for each code snippet, and it automatically adjusts the syntax highlighting and formatting accordingly.

4. Is it possible to include only a specific part of my code in the document?

Yes, it is possible to include only a specific part of your code in the document using the lstinputlisting command. This command allows you to specify the file path of the code snippet you want to include, as well as the starting and ending line numbers, making it easier to insert a specific section of your code.

5. Can I include comments in my code when using the lstlisting package?

Yes, you can include comments in your code when using the lstlisting package. By using the commentstyle option, you can define the style and formatting for comments in your code, making them stand out from the rest of your code and easier to read.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
325
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
41K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • Quantum Physics
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top