How to format the code in the paper and keep it neatly formatted

The code in the paper often appears messy due to misaligned line breaks and missing indentations. This article explains several ways to place code blocks in Word, and how to check whether indentation, line breaks, and characters are preserved after exporting PDF.

Key points of this article

  • Use fixed-width fonts for code and turn off automatic line wrapping so that the indentation can be consistent with the source code.
  • Long lines of code must be manually broken at the appropriate position, and continued lines must be indented one more level to avoid breaking in the middle of identifiers.
  • Separate the code block from the main text: separate paragraphs with light shading, or put them into single-cell tables so that readers can distinguish them at a glance
  • Curly quotation marks and full-width brackets in the code should be replaced with half-width brackets. After exporting the PDF, check the indentation and cross-page position section by section.

When writing a paper, many people will paste a piece of Python or C code directly into Word. As a result, the indentation becomes irregular blanks, a line that is too long is automatically folded to the next line, and the Chinese quotation marks in the comments become curly quotation marks. The reviewing teacher can see at a glance that the publication format is irregular. How to format the code in the paper so that it is neat and easy to read is a very common problem in graduation thesis, course reports and journal submissions.

This article is written for students who need to put code in their papers. After reading, you can choose the right placement method according to the length of the code, follow the steps to set the font, indentation and line breaks in Word, and know where to check after exporting the PDF.

Most general paper formatting tutorials only talk about title levels and page numbers, and rarely cover code, which is sensitive to spaces and line breaks. The following practices all revolve around "whether indentation and line breaks should be retained."

How to format the code in the paper? One sentence conclusion

For short codes, use independent paragraphs with fixed-width fonts; for long codes or codes that need to be highlighted, use single-cell tables with shading; for complete long programs, put them in the appendix, and only quote key parts in the text.

your situationRecommended practicesreason
A few lines of key snippets to be quoted in the textIndependent paragraphs, monospaced font, word wrapping turned offEasy to set up and easy to keep indentation
A complete function in twenty or thirty linesSingle grid table, light shadingClear boundary with the main text, better control when crossing pages
A complete program with hundreds of linesPut it in the appendix and only quote the key parts in the text.Avoid large sections of code interrupting the text, making it easier for reviewers to read
The paper is formatted using LaTeXUse the verbatim environment or code highlighting macro packageIndentation and line breaks are determined by the source file and do not rely on manual adjustments.
There are a lot of Chinese comments in the codeUse text font for comments and keep the code equal width.The character width is inconsistent when Chinese and English are mixed, and it is more tidy to set them separately.

Before you start: Confirm these points first

  • School or journal formatting requirements. Some templates specify the font size, line spacing and shading color of the code. Look at the template first, otherwise you may have to rework the whole thing after it's arranged.
  • Indentation rules for code languages. Python relies on indentation to express code structure. Wrong indentation will change the meaning of the program. Indentation in C-like languages mainly affects readability, but reviewers will still look at it.
  • Whether the code needs to be copied and run. If the reviewer is likely to copy the code to run, keep the original characters and don't let smart quotes, auto-numbering, or full-width symbols get mixed in.
  • Delivery form. Whether you submit in Word or PDF will affect the layout strategy. The display of Word may be slightly different on different computers. PDF can have a fixed layout, which is suitable for finalization.
  • Paper width and line width. The width of the page center determines how many characters can be placed in one line. Estimate the line width first, and then decide which lines need to be broken, which saves time than pasting and then repairing it.

Steps to format code blocks in Word

  1. Unify the indentation characters of source code. Check in the code editor whether the indentation is using spaces or tabs, and convert them all to the same type. Mixing the two is a common cause of indentation misalignment.
  2. Create a new paragraph and set it to a fixed-width font. Select the code and set the font to a fixed-width font such as Consolas or Courier New. Make sure that the width of each character is consistent so that the indentation can be aligned.
  3. Set paragraph format. Select left alignment as the alignment method, and use a fixed value for line spacing. Do not use "multiple line spacing", otherwise the spacing between lines of code will fluctuate.
  4. Manually break super long lines. Break lines between commas, operators, or parameters. Continued lines are indented one more level to show the relationship with the previous line. Don't break in the middle of variable names or keywords.
  5. Use spaces to preserve indentation. Check the blank space at the beginning line by line to confirm that it is consistent with the source code. If you must use Tab, set the tab stop in the paragraph first to avoid display differences on different computers.
  6. Add shading and borders to code blocks. Choose light gray for the shading, and leave a line of space before and after each paragraph to clearly separate the code block from the main text.
  7. Replace characters that should not appear. Use find and replace to change the curly quotes, full-width brackets and full-width commas in the code to half-width. This step is critical, and it is especially error-prone when pasting content from a web page or chat history.
  8. When unified modification is required, it can be processed in the web page editor. Open the UniDoc web page editor (app.unidoc.top), which supports Word-compatible editing, formula and Word format round-trip, and then import back to Word files as needed after modification.

Check each item after completion

Check itemWhat do you think?What if there's a problem?
Whether indentation is retained or notCompare the source code layer by layer to see if each layer of indentation is consistent with the source codeIt is mostly a mixture of Tab and spaces, unified as spaces and then re-pasted.
Is the line break wrong?See if the long line breaks in the middle of the variable name or keywordRe-break the line after the operator or comma and adjust the indentation of the continuation line
Whether the character is replacedSearch for quotation marks and parentheses to see if curved quotation marks or full-width symbols appearChange back to half of the corner with find and replace, or re-paste with plain text instead.
Layout after exportTurn to the position of the code spread to see if a function has been taken apartInsert a page before a paragraph, or change the code to a single table to move as a whole
Code availabilityCopy it out and run it once in the source environmentCorrect the source file when it does not match, and then re-export

# frequently asked questions (FAQ)

1. What if the code is too long to fit a single line?

Manually break lines between operators, commas, or parameters, indent lines one more level, and keep the full text consistent. Don't shrink the font size to illegible in order to keep going.

2. Why is the font of the pasted Chinese comment different from the code?

This is a normal phenomenon. Comments can retain the text font, the code part of the use of equal-width font, the two can be set separately. The width of Chinese and English characters is originally different, but the layout is more orderly after being set separately.

3. Can code screenshots be directly used in papers?

It's not recommended. Screenshots cannot be copied, and it is difficult for judges to check them line by line. Use source text typesetting as much as possible. If there are only pictures or scanned pieces, you can use OCR to identify and then typeset, but the recognition results should be checked line by line with the original code, and machine recognition may confuse the letter O with the number 0, the letter l and the number 1.

4. What is the reason why the exported PDF indentation has changed?

First check to see if spaces and Tab are mixed in Word, and then make sure that the page zoom has not been changed during export. When there is still a problem, changing the code block to a single-grid table and re-exporting it usually stabilizes the layout.

# Summary

The core of the typesetting of the code in the paper is three things: using a font of equal width, keeping indentation with spaces, and separating it from the text with shading or tables. After writing, export the PDF and check it paragraph by paragraph according to the above table, so that the review can see the neat and readable code.

Open OmniDoc to edit and export documents online

Updated 2026-11-12