\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=1.1in]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage[colorlinks=true, linkcolor=blue!60!black, urlcolor=blue!60!black]{hyperref}
\usepackage{mathwrap}
\usepackage{icomma}

\newcommand{\pkg}[1]{\textsf{#1}}
\newcommand{\opt}[1]{\texttt{#1}}
\newcommand{\cs}[1]{\texttt{\textbackslash #1}}
\newcommand{\tuple}[1]{\ensuremath{\mathwrapOff(#1)}}
\newcommand{\cint}[2]{\ensuremath{\mathwrapOff[#1, #2]}}

\makeatletter
\newcommand{\demoIcommaOff}{%
  \let\mathwrap@comma\mathwrap@comma@plain
  \mathwrapOn
}
\makeatother

\title{\pkg{mathwrap}\\[0.35em]
\large Automatic Line-Breaking in Inline Math Mode}
\author{Lee Yiu Sing\\
\small\texttt{yslee.pika@connect.hku.hk}}
\date{November 2026 \quad---\quad Version 1.3}

\begin{document}

\maketitle

\begin{abstract}
\noindent By default, \LaTeX\ strongly resists breaking lines inside inline mathematical expressions (\texttt{\$...\$}), frequently producing \texttt{Overfull \textbackslash hbox} warnings when typesetting long variable lists, tensor indices, or multi-term equations. The \pkg{mathwrap} package enables clean, automatic line breaks after commas in inline math (\cs{textstyle}) and lowers binary and relational operator penalties, while leaving display math and sub-/superscripts unaffected, and integrates seamlessly with \pkg{icomma} for decimal commas.
\end{abstract}

\section{Introduction}

In standard \LaTeX, inline mathematical formulas are treated largely as unbreakable units unless they contain explicit binary or relational operators, and even then high default penalties (\cs{binoppenalty=700}, \cs{relpenalty=500}) discourage line breaks. Comma-separated sequences such as $x_1, x_2, \dots, x_n$ permit no line breaks at all without manual \cs{allowbreak} commands.

\pkg{mathwrap} addresses this globally through three complementary mechanisms:
\begin{enumerate}
  \item \textbf{Active Inline Commas:} Assigns mathcode \texttt{"8000} to the comma so that, exclusively in \cs{textstyle} (via \cs{mathchoice}), a zero-penalty breakpoint and slight stretchable glue (\verb|\penalty0\mskip0mu plus 2mu|) are inserted after each comma. Display equations (\cs{displaystyle}) and sub-/superscripts (\cs{scriptstyle}) never break. When \pkg{icomma} is loaded, only commas followed by a space are treated as breakable punctuation.
  \item \textbf{Relaxed Operator Penalties:} Lowers \cs{binoppenalty} and \cs{relpenalty} to \texttt{100}, allowing \TeX\ to wrap long inline equations at operators such as $+$, $-$, $=$, or $\le$ when needed.
  \item \textbf{Non-Destructive Stretch:} Increases \cs{emergencystretch} to \texttt{1em} (only if currently smaller than \texttt{1em}) so surrounding prose can absorb line breaks smoothly without underfull lines.
\end{enumerate}

\section{Installation}

Place \texttt{mathwrap.sty} in your working directory or install it into your local \texttt{texmf} tree under:
\begin{verbatim}
TEXMFHOME/tex/latex/mathwrap/mathwrap.sty
\end{verbatim}
Run \texttt{mktexlsr} (or \texttt{texhash}) if required by your \TeX\ distribution.

\section{Usage and Package Options}

Load the package in your document preamble:
\begin{verbatim}
\usepackage{mathwrap}
\end{verbatim}
Each of the three wrapping mechanisms can be individually enabled or disabled via package options, summarized in Table~\ref{tab:options}.

\begin{table}[htbp]
  \centering
  \caption{Summary of \pkg{mathwrap} package options.}
  \label{tab:options}
  \vspace{0.4em}
  \begin{tabular}{@{}lll@{}}
    \toprule
    \textbf{Option} & \textbf{Default} & \textbf{Behavior} \\
    \midrule
    \opt{comma}       & Yes & Enables line breaks after commas in \cs{textstyle}. \\
    \opt{nocomma}     & No  & Preserves standard non-breaking math commas. \\
    \opt{penalties}   & Yes & Sets \cs{binoppenalty=100} and \cs{relpenalty=100}. \\
    \opt{nopenalties} & No  & Leaves \cs{binoppenalty} and \cs{relpenalty} untouched. \\
    \opt{stretch}     & Yes & Ensures \cs{emergencystretch} is at least \texttt{1em}. \\
    \opt{nostretch}   & No  & Leaves \cs{emergencystretch} untouched. \\
    \bottomrule
  \end{tabular}
\end{table}

\section{Local Control and Semantic Macros}

The package captures your document's original comma mathcode, any pre-existing active comma definition, and penalty settings at the start of the document (\texttt{begindocument/end}). You can dynamically restore or re-enable them using two mid-document switches:

\begin{itemize}
  \item \cs{mathwrapOff}: Restores the original comma mathcode, active comma definition (such as \pkg{icomma}'s handler), \cs{binoppenalty}, \cs{relpenalty}, and \cs{emergencystretch}.
  \item \cs{mathwrapOn}: Re-activates the \pkg{mathwrap} settings (respecting your package options).
\end{itemize}

\subsection{Comma Protection vs.\ Paragraph Penalties}

Because \TeX\ applies penalties and stretch at the \emph{end of a paragraph}, calling \cs{mathwrapOff} inside a paragraph purely protects the commas within that group. To restore standard line-breaking penalties for an entire block of text, ensure the group includes a blank line or \cs{par}:
\begin{verbatim}
{%
  \mathwrapOff
  Standard paragraph behavior applied here, keeping short relational
  statements together and turning off comma breaks.\par
}
\end{verbatim}

\subsection{Semantic Wrapper Macros}

For elements that should never break (such as short tuples or closed intervals), define lightweight semantic commands using \cs{ensuremath} and local scoping:
\begin{verbatim}
\newcommand{\tuple}[1]{\ensuremath{\mathwrapOff(#1)}}
\newcommand{\cint}[2]{\ensuremath{\mathwrapOff[#1, #2]}}
\end{verbatim}
Usage: \verb|The trajectory passes through \tuple{x_1, x_2, x_3}.|
If you also use European decimal commas (such as $3,14159$), load \pkg{icomma} alongside \pkg{mathwrap} as described in Section~\ref{sec:icomma} rather than wrapping every number manually.

\section{Live Demonstration}

The side-by-side comparison below is typeset directly using \pkg{mathwrap} to illustrate narrow-column behavior before and after activation. Notice how \cs{mathwrapOff} seamlessly restores baseline \TeX\ behavior:

\vspace{0.6em}
\noindent\fbox{%
  \begin{minipage}[t]{0.46\textwidth}
    \small
    \textbf{Without \pkg{mathwrap}}\par\vspace{0.3em}
    {%
      \mathwrapOff
      Consider $e_{11}, e_{12}, e_{13}, e_{14}, e_{21}, e_{22}, e_{23}, e_{24}, e_{31}, e_{32}, e_{33}, e_{34}$ satisfying $a_1 + a_2 + a_3 + a_4 + a_5 = b_1 + b_2 + b_3 + b_4 + b_5$ inside $[0, 1]$.\par
    }
  \end{minipage}%
}\hfill
\fbox{%
  \begin{minipage}[t]{0.46\textwidth}
    \small
    \textbf{With \pkg{mathwrap}}\par\vspace{0.3em}
    Consider $e_{11}, e_{12}, e_{13}, e_{14}, e_{21}, e_{22}, e_{23}, e_{24}, e_{31}, e_{32}, e_{33}, e_{34}$ satisfying $a_1 + a_2 + a_3 + a_4 + a_5 = b_1 + b_2 + b_3 + b_4 + b_5$ inside \cint{0}{1}.\par
  \end{minipage}%
}
\vspace{0.6em}

\noindent Multi-index subscripts such as $R_{i_1, i_2, i_3, i_4}$ and display equations are unaffected:
\[
  f(x_1, x_2, x_3, x_4, x_5, x_6) = \sum_{k=1}^{6} x_k
\]

\section{Compatibility and \pkg{icomma} Integration}
\label{sec:icomma}

\subsection{Intelligent Decimal Commas with \pkg{icomma}}

\pkg{mathwrap} automatically detects \pkg{icomma} regardless of package load order (using \LaTeX's \texttt{begindocument/end} hook, with an \cs{AtBeginDocument} fallback on older kernels):
\begin{verbatim}
\usepackage{mathwrap}
\usepackage{icomma} % load order does not matter
\end{verbatim}
When \pkg{icomma} is active, \pkg{mathwrap} preserves \pkg{icomma}'s single-token lookahead:
\begin{itemize}
  \item \textbf{List commas (followed by a space):} Expressions such as \verb|$a, b, c$| retain normal punctuation spacing and allow line breaks in \cs{textstyle}.
  \item \textbf{Decimal commas (not followed by a space):} Expressions such as \verb|$3,14159$| or \verb|$f(x,y)$| are typeset as \cs{mathord} with no extra space and \emph{never} break across lines.
  \item \textbf{With \cs{mathwrapOff} or \opt{nocomma}:} Disabling comma wrapping restores \pkg{icomma}'s original active-comma handler rather than reverting to a plain comma.
\end{itemize}

\subsection{Demonstration: \pkg{mathwrap} with \pkg{icomma}}

The side-by-side comparison below illustrates how \pkg{icomma} prevents unwanted thin spaces and line breaks inside decimal numbers while preserving \pkg{mathwrap}'s automatic wrapping after list commas:

\vspace{0.6em}
\noindent\fbox{%
  \begin{minipage}[t]{0.46\textwidth}
    \small
    \textbf{Without \pkg{icomma} (\pkg{mathwrap} only)}\par\vspace{0.3em}
    {%
      \demoIcommaOff
      The constant is approximately $3,14159265358979$ and the measured value equals $123,456789012345$, while the sequence $x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8$ wraps at list commas.\par
    }
  \end{minipage}%
}\hfill
\fbox{%
  \begin{minipage}[t]{0.46\textwidth}
    \small
    \textbf{With \pkg{icomma} + \pkg{mathwrap}}\par\vspace{0.3em}
    The constant is approximately $3,14159265358979$ and the measured value equals $123,456789012345$, while the sequence $x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8$ wraps at list commas.\par
  \end{minipage}%
}
\vspace{0.6em}

\noindent When \cs{mathwrapOff} is active, \pkg{icomma} remains intact:
{\mathwrapOff
decimal numbers such as $3,14159$ keep tight spacing, while $(a, b, c)$ retains
standard punctuation spacing.}

\subsection{Other Packages and Environments}

\begin{itemize}
  \item \textbf{Parser-Sensitive Environments (\pkg{TikZ}, \pkg{pgfplots}):} Standard coordinate parsing in \pkg{TikZ} operates outside math mode and works normally. If an inline math expression inside a diagram reacts unexpectedly, wrap the environment in \cs{mathwrapOff} \dots\ \cs{mathwrapOn}.
\end{itemize}

\section{License}

This work may be distributed and/or modified under the conditions of the \LaTeX\ Project Public License (LPPL), version 1.3c or later.

\end{document}
