1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Include source code in latex

  1. Jan 12, 2013 #1
    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 doesnt 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.
    ...
     
  2. jcsd
  3. Jan 12, 2013 #2

    gneill

    User Avatar

    Staff: Mentor

    Code (Text):

    Can't you put
          your source code
          inside code
    tags?
     
     
  4. Jan 12, 2013 #3
    maybe. How do you make code tags?
     
  5. Jan 12, 2013 #4

    gneill

    User Avatar

    Staff: Mentor

    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.
     

    Attached Files:

  6. Jan 12, 2013 #5
    okay but this is for the messages on this site. Does it work in a latex document too? edit: ill just try ofc
     
  7. Jan 12, 2013 #6

    gneill

    User Avatar

    Staff: Mentor

    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: Jan 12, 2013
  8. Jan 12, 2013 #7

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  9. Jan 12, 2013 #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.
    ...
     
  10. Jan 12, 2013 #9
    okay I tried lstlset. Now I get error: "Package keyval error"

    edit: I am now using listingsutf8 with \lstset
     
  11. Jan 12, 2013 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  12. Jan 12, 2013 #11
    sorry that comment was written before I tried your solution. Look my previous comment :)
     
  13. Jan 12, 2013 #12

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    My mind reading skills aren't up to snuff today. How did you use \lstset? Did you read the documentation on it?
     
  14. Jan 12, 2013 #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.
     
  15. Jan 12, 2013 #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.
     
  16. Jan 12, 2013 #15

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You aren't showing the code. How did you use \lstset?
     
  17. Jan 12, 2013 #16
    The cheap way to do it, is the verbatim environment, but it will not have line numbers or syntax highlighting etc.

    Code (Text):

    \begin{verbatim}
    #include <iostream>
     
    using namespace std;
     
    int main(void)
    {
        cout << "hello world\n";
        cin.get();
     
        return 0;
    }
    \end{verbatim}
     
     
  18. Jan 12, 2013 #17

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    I don't think verbatim can handle non-ASCII characters, either.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Include source code in latex
  1. Compiling source code (Replies: 5)

  2. Code Runtime (Replies: 4)

Loading...