Utilities API Reference
utils
Utility functions for LaTeX generation.
hex_to_pgf_rgb(color)
Convert a hex color code to PGF/TikZ RGB format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str
|
A hex color code like "#5D8AA8" or "5D8AA8". |
required |
Returns:
| Type | Description |
|---|---|
str
|
The color in PGF format: "{rgb,255:red,93; green,138; blue,168}" |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the color is not a valid 6-character hex code. |
Examples:
>>> hex_to_pgf_rgb("#5D8AA8")
'{rgb,255:red,93; green,138; blue,168}'
>>> hex_to_pgf_rgb("#FF0000")
'{rgb,255:red,255; green,0; blue,0}'
>>> hex_to_pgf_rgb("00FF00")
'{rgb,255:red,0; green,255; blue,0}'
Source code in src/texer/utils.py
is_hex_color(color)
Check if a string is a valid hex color code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str
|
A string to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the string is a valid hex color code (with or without #). |
Examples:
>>> is_hex_color("#5D8AA8")
True
>>> is_hex_color("FF0000")
True
>>> is_hex_color("blue")
False
>>> is_hex_color("#GGG")
False
Source code in src/texer/utils.py
escape_latex(text)
Escape special LaTeX characters in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to escape. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The escaped text safe for LaTeX. |
Examples:
Source code in src/texer/utils.py
format_option_value(value)
Format a Python value for use in LaTeX options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
LaTeX-formatted string. |
Examples:
>>> format_option_value("north west")
'{north west}'
>>> format_option_value(True)
'true'
>>> format_option_value(3.14)
'3.14'
Source code in src/texer/utils.py
format_options(options, raw_options=None)
Format a dictionary of options for LaTeX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
dict[str, Any]
|
Dictionary of option key-value pairs. |
required |
raw_options
|
str | None
|
Raw LaTeX options string to append. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted options string (without surrounding brackets). |
Examples:
Source code in src/texer/utils.py
indent(text, spaces=2)
Indent each line of text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to indent. |
required |
spaces
|
int
|
Number of spaces to indent. |
2
|
Returns:
| Type | Description |
|---|---|
str
|
Indented text. |
Source code in src/texer/utils.py
wrap_environment(name, content, options='')
Wrap content in a LaTeX environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Environment name. |
required |
content
|
str
|
Content to wrap. |
required |
options
|
str
|
Optional options string. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
Complete environment string. |
Source code in src/texer/utils.py
cmidrule(start, end=None, trim_left=False, trim_right=False, trim_between=False)
Generate \cmidrule command(s) from the booktabs package.
Can generate a single cmidrule or multiple cmidrules from a list of ranges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int | list[tuple[int, int]]
|
Either: - Starting column number (1-indexed) for a single rule, OR - List of (start, end) tuples for multiple rules |
required |
end
|
int | None
|
Ending column number (1-indexed). Required if start is an int. |
None
|
trim_left
|
str | bool
|
Left trim specification. Can be: - False: no left trim - True: default left trim ("l") - str: custom trim width (e.g., "0.5em") |
False
|
trim_right
|
str | bool
|
Right trim specification. Can be: - False: no right trim - True: default right trim ("r") - str: custom trim width (e.g., "0.5em") |
False
|
trim_between
|
bool
|
If True and multiple ranges given, automatically add trim_right to all but the last rule and trim_left to all but the first rule, creating gaps between adjacent rules. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The \cmidrule command string(s), space-separated if multiple. |
Examples:
>>> cmidrule(1, 3)
'\\cmidrule{1-3}'
>>> cmidrule(2, 4, trim_left=True, trim_right=True)
'\\cmidrule(lr){2-4}'
>>> cmidrule([(2, 4), (5, 7)])
'\\cmidrule{2-4} \\cmidrule{5-7}'
>>> cmidrule([(2, 4), (5, 7)], trim_between=True)
'\\cmidrule(r){2-4} \\cmidrule(l){5-7}'
>>> cmidrule([(1, 2), (3, 4), (5, 6)], trim_between=True)
'\\cmidrule(r){1-2} \\cmidrule(lr){3-4} \\cmidrule(l){5-6}'
Source code in src/texer/utils.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |