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

Related Questions

I'm working on a LaTeX document and using lstlisting to display my Java source
What solutions are there for working on a LaTeX document on both Windows and
Working on a website that has Employee and Branch entities, using a database table
Using LaTeX , I need to show some code snippet inside a table. Here
I am working on an enterprise system that will utilise a RESTful web service
I have just begun using LaTeX. I am working in Windows and am using
I'm working on an html5 app that will be used on iPads in a
I am currently working on some LaTeX document which embeds C# files generated by
I'm wondering if there exists a python module that would allow me to do
I've got a working app that serializes a document (of type IDocument) to disk.

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.