| 1 |
--- |
| 2 |
icon: lucide/captions |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_admonition_block]{: .source-link } |
| 5 |
|
| 6 |
# Caption |
| 7 |
|
| 8 |
/// New | New in 10.12 |
| 9 |
/// |
| 10 |
|
| 11 |
## Overview |
| 12 |
|
| 13 |
The Caption extension allows for wrapping blocks in `#!html <figure>` tags and inserting a `#!html <figcaption>` tag |
| 14 |
with specified content. |
| 15 |
|
| 16 |
```py3 |
| 17 |
import markdown |
| 18 |
md = markdown.Markdown(extensions=['pymdownx.blocks.caption']) |
| 19 |
``` |
| 20 |
|
| 21 |
## Usage |
| 22 |
### Adding Captions |
| 23 |
|
| 24 |
Caption is easy to use and simply requires the user to place a caption directly after a block that they'd like to wrap |
| 25 |
in a figure with a caption. Figures can have any content: images, tables, quotes, anything. |
| 26 |
|
| 27 |
```text title="Adding Caption" |
| 28 |
Fruit | Amount |
| 29 |
---------- | ------ |
| 30 |
Apple | 20 |
| 31 |
Peach | 10 |
| 32 |
Banana | 3 |
| 33 |
Watermelon | 1 |
| 34 |
|
| 35 |
/// caption |
| 36 |
Fruit Count |
| 37 |
/// |
| 38 |
``` |
| 39 |
|
| 40 |
//// html | div.result |
| 41 |
Fruit | Amount |
| 42 |
---------- | ------ |
| 43 |
Apple | 20 |
| 44 |
Peach | 10 |
| 45 |
Banana | 3 |
| 46 |
Watermelon | 1 |
| 47 |
|
| 48 |
/// caption |
| 49 |
Fruit Count |
| 50 |
/// |
| 51 |
//// |
| 52 |
|
| 53 |
### Nesting Captions |
| 54 |
|
| 55 |
Captions can be nested, and if multiple captions are used on a single block, the block will be wrapped in multiple |
| 56 |
nested figures. |
| 57 |
|
| 58 |
```text title="Nested Captions" |
| 59 |
 |
| 60 |
|
| 61 |
/// caption |
| 62 |
Inner caption |
| 63 |
/// |
| 64 |
|
| 65 |
/// caption |
| 66 |
Outer caption |
| 67 |
/// |
| 68 |
``` |
| 69 |
|
| 70 |
//// html | div.result |
| 71 |
 |
| 72 |
|
| 73 |
/// caption |
| 74 |
Inner caption |
| 75 |
/// |
| 76 |
|
| 77 |
/// caption |
| 78 |
Outer caption |
| 79 |
/// |
| 80 |
//// |
| 81 |
|
| 82 |
If a caption is provided after a figure element that does not already contain a caption, the caption will be inserted |
| 83 |
into that figure. This allows for more complex figures with sub-figures. |
| 84 |
|
| 85 |
```html title="Complex Captions" |
| 86 |
//// html | figure |
| 87 |
 |
| 88 |
|
| 89 |
/// caption |
| 90 |
Caption 1 |
| 91 |
/// |
| 92 |
|
| 93 |
 |
| 94 |
|
| 95 |
/// caption |
| 96 |
Caption 2 |
| 97 |
/// |
| 98 |
//// |
| 99 |
|
| 100 |
/// caption |
| 101 |
attrs: {class: "general"} |
| 102 |
General caption |
| 103 |
/// |
| 104 |
|
| 105 |
<style> |
| 106 |
figure.general > figcaption { |
| 107 |
font-weight: bold; |
| 108 |
} |
| 109 |
</style> |
| 110 |
``` |
| 111 |
|
| 112 |
///// html | div.result |
| 113 |
//// html | figure |
| 114 |
 |
| 115 |
|
| 116 |
/// caption |
| 117 |
Caption 1 |
| 118 |
/// |
| 119 |
|
| 120 |
 |
| 121 |
|
| 122 |
/// caption |
| 123 |
Caption 2 |
| 124 |
/// |
| 125 |
//// |
| 126 |
|
| 127 |
/// caption |
| 128 |
attrs: {class: "general"} |
| 129 |
General caption |
| 130 |
/// |
| 131 |
|
| 132 |
<style> |
| 133 |
figure.general > figcaption { |
| 134 |
font-weight: bold; |
| 135 |
} |
| 136 |
</style> |
| 137 |
///// |
| 138 |
|
| 139 |
This can be used with Python Markdown's [`md_in_html` extension][md-in-html] as well, but keep in mind that figures |
| 140 |
specified in this way must include the `markdown` attribute or they will be invisible to the Caption extension. Further, |
| 141 |
if `#!html <figcaption>` elements do not have the `markdown` attribute, Caption will not know a figure already has a |
| 142 |
caption. |
| 143 |
|
| 144 |
```html title="Complex Captions: <code>md_in_html</code>" |
| 145 |
<figure markdown> |
| 146 |
 |
| 147 |
|
| 148 |
/// caption |
| 149 |
Caption 1 |
| 150 |
/// |
| 151 |
|
| 152 |
 |
| 153 |
|
| 154 |
/// caption |
| 155 |
Caption 2 |
| 156 |
/// |
| 157 |
</figure> |
| 158 |
|
| 159 |
/// caption |
| 160 |
attrs: {class: "general"} |
| 161 |
General caption |
| 162 |
/// |
| 163 |
|
| 164 |
<style> |
| 165 |
figure.general > figcaption { |
| 166 |
font-weight: bold; |
| 167 |
} |
| 168 |
</style> |
| 169 |
``` |
| 170 |
|
| 171 |
///// html | div.result |
| 172 |
<figure markdown> |
| 173 |
 |
| 174 |
|
| 175 |
/// caption |
| 176 |
Caption 1 |
| 177 |
/// |
| 178 |
|
| 179 |
 |
| 180 |
|
| 181 |
/// caption |
| 182 |
Caption 2 |
| 183 |
/// |
| 184 |
</figure> |
| 185 |
|
| 186 |
/// caption |
| 187 |
attrs: {class: "general"} |
| 188 |
General caption |
| 189 |
/// |
| 190 |
|
| 191 |
<style> |
| 192 |
figure.general > figcaption { |
| 193 |
font-weight: bold; |
| 194 |
} |
| 195 |
</style> |
| 196 |
///// |
| 197 |
|
| 198 |
### Prepending Captions |
| 199 |
|
| 200 |
Captions are appended by default, but can be configured to be prepended by default is desired. Simply set the global |
| 201 |
[`prepend` option](#global-options) to `#!py True`. |
| 202 |
|
| 203 |
```text title="Prepend Caption" |
| 204 |
Fruit | Amount |
| 205 |
---------- | ------ |
| 206 |
Apple | 20 |
| 207 |
Peach | 10 |
| 208 |
Banana | 3 |
| 209 |
Watermelon | 1 |
| 210 |
|
| 211 |
/// caption |
| 212 |
Fruit Count |
| 213 |
/// |
| 214 |
``` |
| 215 |
|
| 216 |
//// html | div.result |
| 217 |
```md-render |
| 218 |
--- |
| 219 |
extensions: |
| 220 |
- pymdownx.blocks.caption |
| 221 |
- tables |
| 222 |
extension_configs: |
| 223 |
pymdownx.blocks.caption: |
| 224 |
prepend: true |
| 225 |
--- |
| 226 |
Fruit | Amount |
| 227 |
---------- | ------ |
| 228 |
Apple | 20 |
| 229 |
Peach | 10 |
| 230 |
Banana | 3 |
| 231 |
Watermelon | 1 |
| 232 |
|
| 233 |
/// caption |
| 234 |
Fruit Count |
| 235 |
/// |
| 236 |
``` |
| 237 |
//// |
| 238 |
|
| 239 |
Regardless of whether `prepend` is enabled or disabled globally, you can force prepend or append per block by specifying |
| 240 |
`<` or `>` in the header for prepending or appending respectively. |
| 241 |
|
| 242 |
```text title="Manual Prepend/Append" |
| 243 |
 |
| 244 |
|
| 245 |
/// caption | < |
| 246 |
Caption 1 |
| 247 |
/// |
| 248 |
|
| 249 |
 |
| 250 |
|
| 251 |
/// caption |
| 252 |
Caption 2 |
| 253 |
/// |
| 254 |
``` |
| 255 |
|
| 256 |
//// html | div.result |
| 257 |
 |
| 258 |
|
| 259 |
/// caption | < |
| 260 |
Caption 1 |
| 261 |
/// |
| 262 |
|
| 263 |
 |
| 264 |
|
| 265 |
/// caption |
| 266 |
Caption 2 |
| 267 |
/// |
| 268 |
//// |
| 269 |
|
| 270 |
### Auto Prefix and IDs |
| 271 |
|
| 272 |
Caption defines multiple caption types by default: a generic `caption`, `figure-caption`, and `table-caption`. By |
| 273 |
default `figure-caption` and `table-caption` are configured with prefix templates, and when used, the figure will have |
| 274 |
special prefixes added to the caption with auto incrementing numbers. Additionally, a corresponding ID will be assigned |
| 275 |
to the figure that wraps the content for linking. |
| 276 |
|
| 277 |
````text title="Auto Prefix and IDs" |
| 278 |
```diagram |
| 279 |
graph TD |
| 280 |
A[Hard] -->|Text| B(Round) |
| 281 |
B --> C{Decision} |
| 282 |
C -->|One| D[Result 1] |
| 283 |
C -->|Two| E[Result 2] |
| 284 |
``` |
| 285 |
/// figure-caption |
| 286 |
Decision Diagram |
| 287 |
/// |
| 288 |
|
| 289 |
Fruit | Amount |
| 290 |
---------- | ------ |
| 291 |
Apple | 20 |
| 292 |
Peach | 10 |
| 293 |
Banana | 3 |
| 294 |
Watermelon | 1 |
| 295 |
|
| 296 |
/// table-caption |
| 297 |
Fruit Count |
| 298 |
/// |
| 299 |
```` |
| 300 |
|
| 301 |
//// html | div.result |
| 302 |
```diagram |
| 303 |
graph TD |
| 304 |
A[Hard] -->|Text| B(Round) |
| 305 |
B --> C{Decision} |
| 306 |
C -->|One| D[Result 1] |
| 307 |
C -->|Two| E[Result 2] |
| 308 |
``` |
| 309 |
/// figure-caption |
| 310 |
Decision Diagram |
| 311 |
/// |
| 312 |
|
| 313 |
Fruit | Amount |
| 314 |
---------- | ------ |
| 315 |
Apple | 20 |
| 316 |
Peach | 10 |
| 317 |
Banana | 3 |
| 318 |
Watermelon | 1 |
| 319 |
|
| 320 |
/// table-caption |
| 321 |
Fruit Count |
| 322 |
/// |
| 323 |
//// |
| 324 |
|
| 325 |
Numbers are tracked separately for each caption type that specifies a prefix template. When a caption is nested under |
| 326 |
other figures, captions will have a dotted number corresponding with the nested level, e.g. 1, 1.1, 1.1.1, etc. |
| 327 |
|
| 328 |
If desired, auto prefixing of nested sub-levels can be restricted via `auto_levels`. When specifying a non-zero, |
| 329 |
positive number, auto-generated IDs and prefixes will be restricted to the specified nesting level. A nesting level of 1 |
| 330 |
only applies IDs and prefixes to the outermost figure of a given type where a nesting level of 2 will apply to the |
| 331 |
outermost figure plus one level of nested children of the same type. |
| 332 |
|
| 333 |
### Increase Nesting Depth |
| 334 |
|
| 335 |
/// warning |
| 336 |
Requires [`auto`](#global-options) to be set to `#!py True` (the default). |
| 337 |
/// |
| 338 |
|
| 339 |
To accommodate various needs, some more control over numbering is exposed. If a user would like to show sub-figure |
| 340 |
numbers but not have the figures directly nested under a parent figure, automatic numbers can be influenced by |
| 341 |
specifying a specific nesting depth in the header of the caption block. By specifying a number prefixed with `^`, we can |
| 342 |
increase the numbering depth for that specific figure by the specified value relative to its current depth. Figures are |
| 343 |
still subject to the `auto_levels` limit. |
| 344 |
|
| 345 |
```text title="Nesting Depth" |
| 346 |
 |
| 347 |
/// figure-caption |
| 348 |
Caption |
| 349 |
/// |
| 350 |
|
| 351 |
 |
| 352 |
/// figure-caption | ^1 |
| 353 |
Caption |
| 354 |
/// |
| 355 |
|
| 356 |
 |
| 357 |
/// figure-caption | ^1 |
| 358 |
Caption |
| 359 |
/// |
| 360 |
|
| 361 |
 |
| 362 |
/// figure-caption |
| 363 |
Caption |
| 364 |
/// |
| 365 |
``` |
| 366 |
|
| 367 |
//// html | div.result |
| 368 |
 |
| 369 |
/// figure-caption |
| 370 |
Caption |
| 371 |
/// |
| 372 |
|
| 373 |
 |
| 374 |
/// figure-caption | ^1 |
| 375 |
Caption |
| 376 |
/// |
| 377 |
|
| 378 |
 |
| 379 |
/// figure-caption | ^1 |
| 380 |
Caption |
| 381 |
/// |
| 382 |
|
| 383 |
 |
| 384 |
/// figure-caption |
| 385 |
Caption |
| 386 |
/// |
| 387 |
//// |
| 388 |
|
| 389 |
/// tip |
| 390 |
Nesting depths can be used with per block prepend notation as long as `<` or `>` comes before the depth notation. |
| 391 |
/// |
| 392 |
|
| 393 |
### Manual Figure Numbers |
| 394 |
|
| 395 |
/// warning |
| 396 |
It is recommended to use [`auto`](#global-options) set to `#!py False`, i.e. "manual mode", but can be used with when it is set |
| 397 |
to `#!py True` with some limitations. |
| 398 |
/// |
| 399 |
|
| 400 |
Captions allows for setting figures numbers for prefixes and IDs on the fly. This is mainly designed for use in "manual |
| 401 |
mode" ([`auto`](#global-options) set to `#!py False`). The idea was to provide a mode where the user has complete control of |
| 402 |
figure numbers if so desired. When in manual mode, the user will only get prefixes when specifying numbers in the header |
| 403 |
of figure types that specify a prefix template. Manual mode does not not check or validate these numbers beyond ensuring |
| 404 |
they are in fact numbers that are formatted properly. |
| 405 |
|
| 406 |
```text title="Manual Numbers" |
| 407 |
 |
| 408 |
/// figure-caption | 12 |
| 409 |
Caption |
| 410 |
/// |
| 411 |
``` |
| 412 |
|
| 413 |
//// html | div.result |
| 414 |
 |
| 415 |
/// figure-caption | 12 |
| 416 |
Caption |
| 417 |
/// |
| 418 |
//// |
| 419 |
|
| 420 |
/// tip |
| 421 |
Manual numbers can be used with per block prepend notation as long as `<` or `>` comes before the number. |
| 422 |
/// |
| 423 |
|
| 424 |
In "automatic mode" (the default) the number will be used if, and only if, the specified number is greater than the last |
| 425 |
figure number (automatic or otherwise) that was used for the specific figure type at the specific number depth. If, for |
| 426 |
any reason, this is not true, an automatic figure number at the specified number depth will be used instead. |
| 427 |
Essentially, the internal, automatic counter can be increased at any time, but can never be decreased. |
| 428 |
|
| 429 |
As an example, if `1.1` is specified, but the last number for current figure type was `2`, `2.1` will be used instead. |
| 430 |
If the number is much bigger, Caption will not care and accept it readily, but it may create a gap in numbering. |
| 431 |
|
| 432 |
Additionally, manual numbers are subject to the `auto_levels` limit when in "automatic mode". |
| 433 |
|
| 434 |
### Static IDs |
| 435 |
|
| 436 |
While automatic mode will generate IDs for prefixed captions, some may prefer an unchanging, static ID for linking. If |
| 437 |
you'd like to link to a figure with a static ID, you can specify one on any figure type (prefixed or non-prefixed) and |
| 438 |
the ID will be applied instead of any auto-generated ID. Manually specified IDs will not affect auto-incrementing |
| 439 |
numerical prefixes. |
| 440 |
|
| 441 |
``` |
| 442 |
 |
| 443 |
|
| 444 |
/// figure-caption |
| 445 |
attrs: {id: static-id} |
| 446 |
caption |
| 447 |
/// |
| 448 |
``` |
| 449 |
|
| 450 |
IDs specified in this way will also override IDs generated in [manual mode](#manual-figure-numbers). |
| 451 |
|
| 452 |
If you prefer a more compact syntax, the ID can also be specified inline in the caption header. Use `#id-value` after |
| 453 |
any prepend marker or manual numbering: |
| 454 |
|
| 455 |
``` |
| 456 |
 |
| 457 |
|
| 458 |
/// figure-caption | #static-id |
| 459 |
caption |
| 460 |
/// |
| 461 |
|
| 462 |
 |
| 463 |
/// figure-caption | < #prepend-id |
| 464 |
caption |
| 465 |
/// |
| 466 |
``` |
| 467 |
|
| 468 |
If using figure numbers and IDs, the figure number notation must come before the ID. |
| 469 |
|
| 470 |
``` |
| 471 |
 |
| 472 |
|
| 473 |
/// figure-caption | 3 #static-id |
| 474 |
caption |
| 475 |
/// |
| 476 |
``` |
| 477 |
|
| 478 |
The inline shorthand behaves the same as the `attrs` front matter and will override any automatically generated ID. It |
| 479 |
is available for all caption types, so the same syntax applies to `table-caption`, custom types, etc. |
| 480 |
|
| 481 |
### Classes and Attributes |
| 482 |
|
| 483 |
Captions also allows for adding arbitrary classes and attributes in addition to normal IDs. Simply chain the attributes |
| 484 |
together as if you were specifying a selector in CSS. |
| 485 |
|
| 486 |
``` |
| 487 |
 |
| 488 |
|
| 489 |
/// figure-caption | 3 #static-id.some-class[data-value="arbitrary value"] |
| 490 |
caption |
| 491 |
/// |
| 492 |
``` |
| 493 |
|
| 494 |
### Configuring Figure Types |
| 495 |
|
| 496 |
While Caption provides a few default figure types, users are free to define their own with different prefixes. |
| 497 |
|
| 498 |
If a figure type is defined with no prefix, that type will never have IDs or prefixes generated for it. The default |
| 499 |
generic `caption` type is an example of a non-prefix caption type. |
| 500 |
|
| 501 |
Users can override the default types with their own via the [`types` option](#global-options). `types` is defined as a list |
| 502 |
of types which can either be a single string specifying the name of the caption type (with no prefix), or a dictionary |
| 503 |
object that specifies, the `name` and `prefix` template for the caption prefix. An empty string for the `prefix` is the |
| 504 |
same as no prefix being specified. `{}` must be specified for non-empty templates and is used to insert the |
| 505 |
auto-generated numerical number of the figure. |
| 506 |
|
| 507 |
```py |
| 508 |
extenconfigs = { |
| 509 |
"pymdownx.blocks.caption": { |
| 510 |
"types": [ |
| 511 |
'caption', |
| 512 |
{ |
| 513 |
'name': 'figure-caption', |
| 514 |
'prefix': 'Figure {}.' |
| 515 |
}, |
| 516 |
{ |
| 517 |
'name': 'table-caption', |
| 518 |
'prefix': 'Table {}.' |
| 519 |
} |
| 520 |
] |
| 521 |
} |
| 522 |
} |
| 523 |
``` |
| 524 |
|
| 525 |
If desired, you can also specify optional classes to be added to a specific figure type via the `classes` option. |
| 526 |
|
| 527 |
```py |
| 528 |
extenconfigs = { |
| 529 |
"pymdownx.blocks.caption": { |
| 530 |
"types": [ |
| 531 |
'caption', |
| 532 |
{ |
| 533 |
'name': 'figure-caption', |
| 534 |
'prefix': 'Figure {}.' |
| 535 |
'classes': 'one-class two-class' |
| 536 |
}, |
| 537 |
{ |
| 538 |
'name': 'table-caption', |
| 539 |
'prefix': 'Table {}.' |
| 540 |
} |
| 541 |
] |
| 542 |
} |
| 543 |
} |
| 544 |
``` |
| 545 |
|
| 546 |
Prefix templates are inserted exactly as specified, that means you can also use raw HTML. That also means you must |
| 547 |
HTML escape anything that needs it as well. |
| 548 |
|
| 549 |
```py |
| 550 |
extenconfigs = { |
| 551 |
"pymdownx.blocks.caption": { |
| 552 |
"types": [ |
| 553 |
'caption', |
| 554 |
{ |
| 555 |
'name': 'figure-caption', |
| 556 |
'prefix': 'Figure <span class="prefix-fig-num">{}</span>.' |
| 557 |
'classes': 'one-class two-class' |
| 558 |
}, |
| 559 |
{ |
| 560 |
'name': 'table-caption', |
| 561 |
'prefix': 'Table <span class="prefix-fig-num">{}</span>.' |
| 562 |
} |
| 563 |
] |
| 564 |
} |
| 565 |
} |
| 566 |
``` |
| 567 |
|
| 568 |
/// new | New 10.16 |
| 569 |
Using raw HTML in prefix templates was added in 10.16. |
| 570 |
/// |
| 571 |
|
| 572 |
## Global Options |
| 573 |
|
| 574 |
Options | Type | Descriptions |
| 575 |
------------- | ---------- | ------------ |
| 576 |
`types` | list | A list of figure types. Figure types must specify a name and can specify an optional prefix template where `{}` is where the figure number will be inserted. |
| 577 |
`prepend` | boolean | Prepend captions opposed appending captions to figures. Disabled by default. |
| 578 |
`auto` | boolean | Auto add IDs to all figure types that include prefix templates. Enabled by default. |
| 579 |
`auto_level` | integer | Limit auto prefixing to a certain depth. Default is 0 which disables limiting. |
| 580 |
|
| 581 |
## Per Block Options |
| 582 |
|
| 583 |
Options | Type | Descriptions |
| 584 |
------------ | ---------- | ------------ |
| 585 |
`attrs` | dictionary | A string that defines attributes for the outer, wrapper element. |