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

  • SEARCH
  • Home
  • 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

Working with a latex document with eps images as in the example below... \documentclass[11pt]{paper}
What solutions are there for working on a LaTeX document on both Windows and
I am working on an enterprise system that will utilise a RESTful web service
I'm refactoring a large javascript document that I picked up from an open source
We're working on an application that displays information through a Direct3D visualisation. A late
Working with a SqlCommand in C# I've created a query that contains a IN
Working on a project that parses a log of events, and then updates a
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
I've been using LaTeX since 2005 with the TeXLive distribution and I've been upgrading

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.