View source: R/ltx_wrap_table_math.R
ltx_wrap_table_math | R Documentation |
This function takes a LaTeX table as input and wraps all numbers, both
positive and negative, in LaTeX math mode ($number$
) only if they are located
within a tabular
or tabularx
environment. Numbers that are already
wrapped in math mode are ignored.
ltx_wrap_table_math(latex_table, ruler_based = TRUE, print_tbl = FALSE)
latex_table |
A character vector or list containing the LaTeX table input. |
ruler_based |
A logical value indicating whether the relevant area of
the table to wrap in math mode is between midrule and bottomrule. Defaults
to |
print_tbl |
A logical value indicating whether to print the table to
the console after modification. Defaults to |
The function handles both integers and decimal numbers, optionally
including commas as thousand separators and supports scientific notation.
It also ensures that numbers followed by superscripts (e.g., 10^{2}
)
are fully wrapped in math mode.
The function identifies numbers inside tabular
or tabularx
environments
and wraps them in dollar signs for LaTeX math mode. It uses a regular expression
to find numbers and ignores those that are already wrapped in math mode.
Explanation of Regular Expression:
(?<!\\$)-?\\b(\\d{1,3}(,\\d{3})*(\\.\\d+)?(e-?\\d+)?)(\\^\\{[^\\}]+\\}\\S*)?(\\S*\\b)?(?![^$]*\\$)
(?<!\\$)
A negative lookbehind that ensures there is no dollar sign ($
) before the current position.
Prevents matching numbers already enclosed in LaTeX math mode ($...$
).
-?
Optional negative sign.
\\b
A word boundary, ensuring that the number is not part of a larger word.
Matches standalone numbers.
(-?\\d{1,3}(,\\d{3})*(\\.\\d+)?(e-?\\d+)?)
Matches numbers in the format:
\\d{1,3}
: First group of digits.
(,\\d{3})*
: Thousands groups separated by commas.
(\\.\\d+)?
: Optional decimal numbers.
(e-?\\d+)?
: Optional scientific notation.
(\\^\\{[^\\}]+\\}\\S*)?
Matches LaTeX superscript notation (e.g., 10^{2}
).
(\\S*\\b)?
Optionally matches any trailing characters after the number.
(?![^$]*\\$)
A negative lookahead to ensure there's no dollar sign ahead in the string (prevents matching numbers already enclosed in LaTeX math mode).
A modified LaTeX table where all numbers within tabular
environments
are wrapped in math mode. The result is returned invisibly.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.