Skip to content

Figure.paragraph: Typeset one or multiple paragraphs #3710

Closed

Description

@seisman

This issue serves as the central place for discussing and tracking the implementation of the Figure.paragraph method in PyGMT. The issue will be closed when the initial implementation is complete. Progress is tracked at PyGMT: Wrapping GMT modules.

Figure.paragraph is a high-level wrapper of GMT's text module -M option to typeset one or multiple paragraphs in a plot.

Documentation

GMT Option Flags and Modifieres

☑️: Implemented; ⬜: To be implemented/discussed; Strikethrough: Won't implement.

Notes on Input Formats

text's -M option means "paragraph mode". In this mode, the input file must be multi-segment files. Segments are separated by a special record whose first character must be flag [Default is >]. Multiple paragraphs are separated with a blank line. Its format is like:

> x y [font angle justify] linespace parwidth parjust
text for paragraph1

text for paragraph2
> x y [font angle justify] linespace parwidth parjust
text for paragraph3
  • x/y: position of the paragraph
  • font/angle: font and angle of the text [Requires -F]
  • justify: justification of the paragraph box [Requires -F]
  • linespace: line spacing
  • parwidth: paragraph width
  • parjust: text justification in the paragraph, can be l(eft), c(enter), r(ight), or j(ustified)

Notes:

  1. Multiple paragraphs should be separated by a blank line when passing to GMT. A sequence of string is the most Pythonic way to represent multiple paragraphs.
  2. When typesetting multiple paragraphs, each paragraph can have its own font/angle/justify/linespace/parwidth/parjust. It means the corresponding parameters should accept a sequence as input. I think it will makes codes much complicated, so prefer not to support it. Users who need different styles for different paragraphs can call Figure.paragraph multiple times.
  3. Passing a plaintext file is not supported. I prefer not to support it anyway.

Linked Pull Requests

Related Issues and Discussions

Activity

  1. added
    feature
    on Jan 5, 2025
  2. added this to the 0.16.0 milestone on Jan 9, 2025
  3. removed this from the 0.16.0 milestone on Mar 31, 2025
  4. added
    feature request
    and removed
    feature
    on Oct 21, 2025
  5. added this to the 0.19.0 milestone on Apr 2, 2026
  6. seisman commented on Apr 27, 2026

    @seisman
    MemberAuthor

    As found in #4602, the GMT behavior for whitespaces is a little weird in PyGMT. Here is a GMT CLI script to understand the behavior:

    gmt text -R-1/1/-1/1 -JX20c/15c -Baf -F+f10p,8 -W1p -M -pdf map << EOF
    > 0 0 10p 10c l
      Paragraph 1: Leading whitespaces are ignored. xxx yyy zzz.
    
    Paragraph 2: This paragraph has two trailing whitespaces, but it's ignored and has no effect. xxx yyy zzz.
    
    Paragraph 3: Multile    whitespaces inside a paragraph are combined into one whitespace. xxx yyy zzz.
    
    	Paragraph 4: One leading tab results in one indentation. xxx yyy zzz.
    
    		Paragraph 5: Two leading tabs are combined into one single tab. xxx yyy zzz.
    
    Paragraph 6: This paragraph has two trailing tabs. xxx yyy zzz. It has no effect. xxx yyy zzz.
    
    Paragraph 7: Multile			tabs inside a paragraph are converted to multiple spaces. xxx yyy zzz.
    
    Paragraph 8: Mixed tabs and spaces. xxx yyy zzz		   	 	aaa bbb ccc.
    
    \nParagraph 9: Leading newline is converted to a space. xxx yyy zzz.
    
    \n\nParagraph 10: Multiple leading newline are converted to multiple spaces. xxx yyy zzz.
    
    Paragraph 11: Trailing newlines are converted to spaces, not starting a new paragraph. xxx yyy zzz.\n\n
    Paragraph 12: Newlines in a paragraph\n\nare converted to spaces.
    
    Paragraph 13: This is the last paragraph. xxx yyy zzz. xxx yyy zzz.
    EOF

    Here is the corresponding PyGMT script:

    import pygmt
    
    text = [
        "  Paragraph 1: Leading whitespaces are ignored. xxx yyy zzz.",
        "Paragraph 2: This paragraph has two trailing whitespaces, but it's ignored and has no effect. xxx yyy zzz.  ",
        "Paragraph 3: Multile    whitespaces inside a paragraph are combined into one whitespace. xxx yyy zzz.",
        "	Paragraph 4: One leading tab results in one indentation. xxx yyy zzz.",
        "		Paragraph 5: Two leading tabs are combined into one single tab. xxx yyy zzz.",
        "Paragraph 6: This paragraph has two trailing tabs. xxx yyy zzz. It has no effect. xxx yyy zzz.		",
        "Paragraph 7: Multile			tabs inside a paragraph are converted to multiple spaces. xxx yyy zzz.",
        "Paragraph 8: Mixed tabs and spaces. xxx yyy zzz		   	 	aaa bbb ccc.",
        "\nParagraph 9: Leading newline is converted to a space. xxx yyy zzz.",
        "\n\nParagraph 10: Multiple leading newline are converted to multiple spaces. xxx yyy zzz.",
        "Paragraph 11: Trailing newlines are converted to spaces, not starting a new paragraph. xxx yyy zzz.\n\n",
        "Paragraph 12: Newlines in a paragraph\n\nare converted to spaces.",
        "Paragraph 13: This is the last paragraph. xxx yyy zzz. xxx yyy zzz."
    ]
    
    fig = pygmt.Figure()
    
    fig.basemap(region=[-1, 1, -1, 1], projection="X20c/15c", frame=True)
    fig.paragraph(text=text, x=0, y=0, parwidth="10c", font="10p,8", linespacing="10p", pen="1p")
    
    fig.show()
    fig.savefig("test.png")

    Here are the outputs:

    GMT CLI Current PyGMT PyGMT in PR #4605
    Image Image Image

    Summary of the behavior in GMT:

    • Leading and trailing spaces are ignored.
    • Multiple spaces inside a paragraph are combined into one single space.
    • Leading tabs are combined into one tab that results in a 4-space indentation.
    • Trailing tabs are ignored.
    • Multiple tabs inside a paragraph are converted to multiple spaces.
    • Mixing tabs and spaces inside a paragraph has a complicated behavior.
    • Newline characters are always converted into spaces.
  7. seisman commented on Apr 28, 2026

    @seisman
    MemberAuthor

    As shown in the examples above, the behaviors of GMT and PyGMT differ substantially. It is important to note, however, that the GMT CLI reads text from plain files, whereas PyGMT reads text from virtual files.

    In practice, I find both GMT’s behavior and the current behavior in PyGMT less than ideal. Text strings are typeset in unexpected ways which likely will surprise and confuse users. It's also difficult to achieve precise layouts, for example, when reproducing code-like formatting (see: https://github.com/GenericMappingTools/pygmt-paper-figures/blob/main/Fig1_PyGMT_GMT_comparison.ipynb).

    In my view, users should be responsible for the content they provide to PyGMT, and PyGMT should render text exactly as given, i.e., following a WYSIWYT (“what you see is what you type”) principle.

    Edit: I'll open a POC PR for it. POC PR at #4605. The result is

    Image

    The new behaviors implemented in PR #4605 are:

    • Trailing spaces/tabs/newlines are removed
    • Leading spaces/tabs are preserved.
    • Each tab is typeset as four spaces, but can be changed by tab_width
    • Newline \n makes no sense inside a paragraph, so it's removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions