Skip to content

Alignment doesn't work in figure caption #1498

Closed

Description

@paulkre

Description

When trying to change the alignment of a figure's caption with

#figure(
  image("..."),
  caption: [
    #set align(left)
    This is the caption.
  ],
)

it will keep the "Figure 1:" label in the center and put everything else on a separate line.

It should be possible to customize the alignment of the entire caption.

Reproduction URL

No response

Operating system

macOS

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jun 14, 2023
  2. added
    styling
    on Jul 3, 2023
  3. antonWetzel commented on Jul 24, 2023

    @antonWetzel
    Contributor

    A (temporary) solution could be to overwrite the figure command.

    Result

    • 1 and 2 without changes
    • 3 and 4 with custom command

    image

    Command

    #show figure: it => box(width:100%)[
    	#align(center)[#it.body]
    	#v(if it.has("gap") {it.gap} else {0.65em})
    	#set align(left)
    	#set par(hanging-indent: 1cm, justify: true)
    	#pad(x: 1cm)[#it.supplement #it.counter.display(it.numbering): #it.caption]
    ]

    Explanation

    • show figure: it => (...)
      • replace the figure command with this function and get the parameters as it
    • box(width: 100%)[...]
      • create a box with the full page width
        • allows to align to center or left
        • stops pagebreaks between image and caption
    • #align(center)[#it.body]
      • display the image centered
    • #v(10pt, weak: true)
      • space between image and caption
    • #set align(left) and #set par(hanging-indent: 1cm, justify: true)
      • control how the caption looks
    • #pad(x: 1cm)[...]
      • pad left and right of the caption
    • #it.supplement
      • what this is ("figure", ...)
    • #it.counter.display(it.numbering)
      • get the figure counter in the wanted style
    • #it.caption
      • finally the image caption

    Full code for example

    #let fake_body = box(width: 70%, height: 15%, fill: orange, stroke: black, radius: 10pt)
    
    #figure(caption: [short caption], fake_body)
    #figure(caption: [#lorem(30)], fake_body)
    
    #show figure: it => box(width:100%)[
    	#align(center)[#it.body]
    	#v(if it.has("gap") {it.gap} else {0.65em})
    	#set align(left)
    	#set par(hanging-indent: 1cm, justify: true)
    	#pad(x: 1cm)[#it.supplement #it.counter.display(it.numbering): #it.caption]
    ]
    
    #figure(caption: [short caption], fake_body)
    #figure(caption: [#lorem(30)], fake_body)

    Notes

    • the style with hanging indent and padding is what I prefer, any styling could be used instead
    • this overwrites other show figure: ... commands
    • the image is cropped, the real pdf has more left and right padding
  4. Andrew15-5 commented on Sep 14, 2023

    @Andrew15-5
    Contributor

    I also noticed the lack of caption alignment. The new figure.caption still doesn't have it. Instead, I had to do this:

    show figure.where(kind: table): fig => {
      set figure.caption(position: top)
      align(right, fig.caption)
      align(center, fig.body)
    }
    

    Previously there was an "incident" where after making a custom show rule for the figure, the caption would be able to be typeset on a separate page from the body (#2109). But at least in my personal situation, the code above works fine.

  5. Andrew15-5 commented on Sep 14, 2023

    @Andrew15-5
    Contributor

    Because the alignment isn't yet supported (in the set rules) for figures (and its caption) I think this should be a feature request. But it also should've been supported from the start so the bug also works, I suppose.

  6. laurmaedje commented on Sep 14, 2023

    @laurmaedje
    Member

    The fact that #show figure.caption: set align(left) doesn't work, is quite clearly a bug to me.

  7. Andrew15-5 commented on Sep 14, 2023

    @Andrew15-5
    Contributor

    The fact that #show figure.caption: set align(left) doesn't work, is quite clearly a bug to me.

    Oh yeah, I either forgot to test this one or I tested it, it didn't work, and I forgot about this. Anyway, yes, this doesn't work, unfortunately, but it clearly should (at least in v0.8).

  8. antonWetzel commented on Sep 15, 2023

    @antonWetzel
    Contributor

    Sorry if I missunderstood something.
    With typst 0.8.0 (464dac8d) I think the aligment works.

    Result

    image

    Code

    #set align(center)
    
    #lorem(20)
    
    #let test() = figure(caption: lorem(20), rect(
    	width: 80%,
    	height: 2cm,
    	fill: orange,
    	radius: 10pt,
    ))
    
    #{
    	test()
    }
    
    #{
    	show figure.caption: it => align(left, it)
    	test()
    }
    
    #{
    	show figure.caption: set align(left)
    	test()
    }
    
    #{
    	show figure.caption: it => pad(
    		left: 1cm,
    		right: 2cm,
    		align(left, par(hanging-indent: 1cm, justify: true, it)),
    	)
    	test()
    }
    
    #lorem(20)
    
  9. laurmaedje commented on Sep 15, 2023

    @laurmaedje
    Member

    It works when the caption is long enough, but if it's short then it's internally correctly aligned, but the whole figure block will be centered. Perhaps that's correct behaviour after all, not really sure.

  10. antonWetzel commented on Sep 15, 2023

    @antonWetzel
    Contributor

    Maybe a width property for figures could work. auto for the current behaviour and 100% to use all the available space (more space as the figure body requires), so the caption can align to the "real" left.

  11. laurmaedje commented on Sep 15, 2023

    @laurmaedje
    Member

    #show figure: set block(width: 100%) should work. Similarly to #show figure: set block(breakable: true).

  12. changed the title [-]Figure caption alignment not customizable[/-] [+]Alignment doesn't work in figure caption[/+] on Nov 14, 2023
  13. maxfreu commented on Nov 29, 2023

    @maxfreu

    Left-aligned, hanging indent justified with the colon would be nice. But I think that's a bit complicated to achieve due to the varying numbering etc... Left align now seems to work nicely.

  14. laurmaedje commented on May 28, 2024

    @laurmaedje
    Member

    #show figure.caption: set align(left) works as expected, so I'll close this.

  15. massanf commented on Jan 18, 2025

    @massanf

    I may be misunderstanding something, but I think that even though this issue is closed the problem that @paulkre pointed out still remains in 0.12.0.

    As @laurmaedje pointed out, setting #show figure.caption: set align(left) does work as expected. However, writing something like:

    #figure(
      image("figures/bird_flock.jpg", width: 90%),
      caption: 
        [
          #set align(left)
          This is a very long caption. This is a very long caption.
          This is a very long caption. This is a very long caption.
          This is a very long caption. This is a very long caption.
          This is a very long caption. This is a very long caption.
        ]
    )

    still results in misaligned supplement:

    Image

    This is problematic for me because I only want to apply align(left) to only some of the figures (with long captions).

    Are these two ways of setting align(left) equivalent?

  16. laurmaedje commented on Jan 18, 2025

    @laurmaedje
    Member

    This is actually working as intended here. It's simply not equivalent. But you can get what you want by using the rule I posted, but scoped to a code or content block containing the figure.

  17. Mental-Inferno commented on Jul 4, 2026

    @Mental-Inferno

    I am still getting this issue. However, adding a #h(1fr) to the end of the caption worked as a stop gap fix.

    Image Image
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

    bugstyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions