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 274879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:40:40+00:00 2026-05-12T00:40:40+00:00

I am working on a latex document that will require typesetting significant amounts of

  • 0

I am working on a latex document that will require typesetting significant amounts of python source code. I’m using pygments (the python module, not the online demo) to encapsulate this python in latex, which works well except in the case of long individual lines – which simply continue off the page. I could manually wrap these lines except that this just doesn’t seem that elegant a solution to me, and I prefer spending time puzzling about crazy automated solutions than on repetitive tasks.

What I would like is some way of processing the python source code to wrap the lines to a certain maximum character length, while preserving functionality. I’ve had a play around with some python and the closest I’ve come is inserting \\\n in the last whitespace before the maximum line length – but of course, if this ends up in strings and comments, things go wrong. Quite frankly, I’m not sure how to approach this problem.

So, is anyone aware of a module or tool that can process source code so that no lines exceed a certain length – or at least a good way to start to go about coding something like that?

  • 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-12T00:40:41+00:00Added an answer on May 12, 2026 at 12:40 am

    You might want to extend your current approach a bit, but using the tokenize module from the standard library to determine where to put your line breaks. That way you can see the actual tokens (COMMENT, STRING, etc.) of your source code rather than just the whitespace-separated words.

    Here is a short example of what tokenize can do:

    >>> from cStringIO import StringIO
    >>> from tokenize import tokenize
    >>> 
    >>> python_code = '''
    ... def foo(): # This is a comment
    ...     print 'foo'
    ... '''
    >>> 
    >>> fp = StringIO(python_code)
    >>> 
    >>> tokenize(fp.readline)
    1,0-1,1:    NL  '\n'
    2,0-2,3:    NAME    'def'
    2,4-2,7:    NAME    'foo'
    2,7-2,8:    OP  '('
    2,8-2,9:    OP  ')'
    2,9-2,10:   OP  ':'
    2,11-2,30:  COMMENT '# This is a comment'
    2,30-2,31:  NEWLINE '\n'
    3,0-3,4:    INDENT  '    '
    3,4-3,9:    NAME    'print'
    3,10-3,15:  STRING  "'foo'"
    3,15-3,16:  NEWLINE '\n'
    4,0-4,0:    DEDENT  ''
    4,0-4,0:    ENDMARKER   ''
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

When coding new javascript heavy websites, which order or web browser do you code
I am working on generating a document for printing. It should use a specific
Wrote a small app that accesses a bunch of search websites and puts the
I am working on a Math related web page and am looking for a
I am working on a project that involves a lot of immutable classes. We

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.