% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{latex_wrap}
\alias{latex_wrap}
\title{Wrap standard text for math-first LaTeX renderers}
\usage{
latex_wrap(tex, input_mode = c("mixed", "math"))
}
\arguments{
\item{tex}{\code{character}. The string or vector of strings to be processed.}

\item{input_mode}{\code{character}. A length-one character vector dictating the
parsing strategy. If \code{"mixed"} (default), the string is tokenized and text
is wrapped. If \code{"math"}, the parser is bypassed and the string is returned
unmodified, assuming the user has provided a pure math equation.}
}
\value{
A \code{character} vector of the same length as \code{tex}, formatted for
math-mode LaTeX rendering.
}
\description{
Parses character strings to safely isolate standard natural language from
LaTeX math environments. Standard text is wrapped in \verb{\\text\{\}} blocks, while
equations, display math, and specific LaTeX environments are preserved verbatim.
This is heavily optimized for passing mixed-content strings (like plot titles
or axis labels) to pure-math typesetting engines like MicroTex. The conversion
is not perfect, but it should handle most common cases without user intervention.
}
\details{
\code{latex_wrap()} operates as a state-machine tokenizer to ensure that valid LaTeX
math is not corrupted by the text-wrapping process. It features:
\itemize{
\item \strong{Delimiter Preservation}: Standard inline (\code{$}, \verb{\\(}) and block (\verb{$$}, \verb{\[})
math delimiters are recognized and preserved.
\item \strong{Environment Tracking}: Complex nested environments (e.g., \verb{\\begin\{matrix\}})
are safely extracted and bypassed.
\item \strong{Newline Conversion}: R newline characters (\verb{\\n}) occurring outside of math
environments are automatically converted to LaTeX line breaks (\verb{\\\\}) inside
the \verb{\\text\{\}} wrapper.
\item \strong{Literal Escapes}: Escaped LaTeX literals (e.g., \verb{\\$}, \verb{\\\%}, \verb{\\#}) are
safely passed into the \verb{\\text\{\}} block without triggering math modes. The
escape character for \verb{\\$} is automatically resolved for MicroTex compatibility.
}
}
\examples{
# "mixed" mode (default) safely wraps text and preserves inline math
latex_wrap(r"(The equation \(E=mc^2\) is famous)")

# "mixed" mode handles user-escaped characters seamlessly
latex_wrap(r"(Cost: \$100 for $x$ items)")

# "mixed" mode converts R newlines to stacked text blocks
latex_wrap(r"(Line 1\nLine 2)")

# "math" mode returns the string completely unmodified
latex_wrap(r"(\frac{\alpha}{\beta})", input_mode = "math")
}
