ltx_wrap_table_math: Wrap Numbers in LaTeX Table in Math Mode within Tabular...

View source: R/ltx_wrap_table_math.R

ltx_wrap_table_mathR Documentation

Wrap Numbers in LaTeX Table in Math Mode within Tabular Environments

Description

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.

Usage

ltx_wrap_table_math(latex_table, ruler_based = TRUE, print_tbl = FALSE)

Arguments

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 TRUE. If FALSE the area of the table wrapped in math mode is between the start and end of the tabular environment.

print_tbl

A logical value indicating whether to print the table to the console after modification. Defaults to FALSE.

Details

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)?(?![^$]*\\$)⁠

  1. ⁠(?<!\\$)⁠

    • A negative lookbehind that ensures there is no dollar sign ($) before the current position.

    • Prevents matching numbers already enclosed in LaTeX math mode (⁠$...$⁠).

  2. ⁠-?⁠

    • Optional negative sign.

  3. ⁠\\b⁠

    • A word boundary, ensuring that the number is not part of a larger word.

    • Matches standalone numbers.

  4. ⁠(-?\\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.

  5. ⁠(\\^\\{[^\\}]+\\}\\S*)?⁠

    • Matches LaTeX superscript notation (e.g., 10^{2}).

  6. ⁠(\\S*\\b)?⁠

    • Optionally matches any trailing characters after the number.

  7. ⁠(?![^$]*\\$)⁠

    • A negative lookahead to ensure there's no dollar sign ahead in the string (prevents matching numbers already enclosed in LaTeX math mode).

Value

A modified LaTeX table where all numbers within tabular environments are wrapped in math mode. The result is returned invisibly.


AndreSjuve/texaid documentation built on Oct. 20, 2024, 6:17 a.m.