Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4600756
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:43:45+00:00 2026-05-21T23:43:45+00:00

I am writing a program (in C, but I suppose that’s not so relevant)

  • 0

I am writing a program (in C, but I suppose that’s not so relevant) in connection with a little documentary material in LaTeX. I want the documentary material to contain code snippets from my original code.

In order to include source code and keep it up to date, I do the following in my document:

\lstinputlisting[firstline=200, lastline=210]{../src/source.c)

This loads automatically the lines 200 to 210 (which contain e.g. a function) from ../src/source.c into my document.

However, if I add some lines before line 200, this means that line 200 “wanders down some lines”, so I have to adjust this in order to get my original function.

So here’s my question: Does anybody know about a possibility how to dynamically tell lstinputlisting (or any adequate substitute) to tell which lines to take?

I imagine something like the following: I add special comments to my C source code that will be recognized by lstinputlisting, e.g.

/// lstinputlisting "myfunc" BEGIN
int myFunction(int x){
  return x+2;
}
/// lstinputlisting "myfunc" END

Then, lstlisting scans the file and just includes the lines between the BEGIN and the END things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:43:46+00:00Added an answer on May 21, 2026 at 11:43 pm

    I am answering a few months after your post, but the feature of lstlistings I describe below has been in that package for several years.

    The keyword to look for is the option linerange, as well as, for convenience, rangeprefix and rangesuffix.

    Here is a complete example.

    \documentclass{article}
    \usepackage{fullpage,times,listings}
    \lstloadlanguages{C++}
    
    \lstset{language=C++,keywordstyle=\bfseries,commentstyle=\itshape,
      rangeprefix=//----------------,rangesuffix=----------------,
      includerangemarker=false,columns=spaceflexible}
    
    \begin{document}
    We first show the main function.
    \lstinputlisting[linerange=main0-main1]{code.cpp}
    Then we show the implementation.
    \lstinputlisting[linerange=fact0-fact1]{code.cpp}
    \end{document}
    

    then save the following in code.cpp:

    #include <cassert>
    
    //----------------fact0----------------
    // A recursive implementation of factorial
    int factorial(int i)
    {
        if(i)
            return i * factorial(i-1);
        else
            return 1;
    }
    //----------------fact1----------------
    
    //----------------main0----------------
    // Test the implementation.
    int main()
    {
        assert(factorial(5) == 120);
    }
    //----------------main1----------------
    

    This is a good solution because one inevitably edits the code, and then it becomes tedious to update the line numbers throughout one’s TeX files. Using symbols solves this problem, but it also leaves a trace in the code itself that if the number of lines changes, or if the width changes too much, one needs to confirm that the typeset output still looks reasonable.

    Finally, after editing the code, you need to typeset the latex files again only if you inserted/deleted within the marked blocks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total newbie, but I was writing a little program that worked on
I'm writing a program that sends an email out at a client's specific local
If you are writing a program that is executable from the command line, you
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
I am writing a program in Python that will act as a server and
I'm writing a program that contains a generational garbage collector. There are just two
I'm writing a program that asks the user to enter their birth date. For
I am writing a program that reads the input from a file and then
I'm writing a C++ program that has a large number of classes. In my
HI I'm writing a program that acts as a server and has the ability

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.