1

I need either to add a color background or border around each inline formula. How?

I want to ensure every formula is no more wide that about the 1/2 of the width of text, in order to publish them in Kindle, as I describe here.

porton
  • 1,218
  • 1
    Please tell us how you initiate and terminate inline math mode: with $...$ or with \(...\)? – Mico Nov 11 '19 at 15:56
  • 1
    Please also tell us which engine you employ to compile your document: pdfTeX, XeTeX, LuaTeX, or something else? – Mico Nov 11 '19 at 15:57
  • 1
    @Mico Currently I use pdfTeX but may switch to XeTeX – porton Nov 11 '19 at 16:51
  • 1
    @Mico I use $...$, but switching to \(...\) is possible (howeever requires a big amount of work) – porton Nov 11 '19 at 16:52
  • Related: https://tex.stackexchange.com/q/135053/18083 and https://tex.stackexchange.com/q/113795/18083 – porton Nov 11 '19 at 17:45
  • I don't know how to come up with a solution based on the capabilities of pdfTeX or XeTeX. However, it's now that hard to do, really, when using LuaTeX, thanks to Lua's library of powerful string functions. The answer I posted below works with both $...$and \(...\). – Mico Nov 11 '19 at 19:01

1 Answers1

2

Here's a LuaLaTeX-based solution. It consists of two LaTeX macros called \InlineMathColorOn and \InlineMathColorOff, which act as switches to activate and deactivate the Lua function called inlinemath_yellow. The Lua function first sets aside cases of \$ and $$, then encases all instances of $...$ and \(...\) in yellow boxes, and finally restores the cases of \$ and $$. The Lua function operates through the process_input_buffer callback, which does its job at a very early stage of processing, well before TeX gets to apply its eyes, mouth, etc.

What this code doesn't do is allow automatic line breaking inside inline-math formualas. But then, you're not going to allow many such constructs in your book, are you?

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{xcolor}  % for '\colorbox' macro
\usepackage{luacode} % for 'luacode' environment

\begin{luacode}
function inlinemath_yellow ( s )
  s = s:gsub ( "\\%$" , "@@@@@@@@@@@@" )  -- instances of "\$"
  s = s:gsub ( "%$%$" , '""""""""""""' )  -- instances of "$$"
  s = s:gsub ( "(%$..-%$)" , function ( u )
           return "\\colorbox{yellow}{$"..u:sub(2,-2).."\\strut$}" 
      end )
  s = s:gsub ( "(\\%(..-\\%))" , function ( u )
         return "\\colorbox{yellow}{\\("..u:sub(3,-3).."\\strut\\)}" 
      end )  
  s = s:gsub ( "@@@@@@@@@@@@" , "\\$" ) -- revert instances of "\$"
  s = s:gsub ( '""""""""""""' , "$$" )  -- revert instances of "$$"
  return s
end
\end{luacode}

%% LaTeX-side code:
\newcommand\InlineMathColorOn{\directlua{%
  luatexbase.add_to_callback ( "process_input_buffer" , 
  inlinemath_yellow , "InlineMathYellow" )}}
\newcommand\InlineMathColorOff{\directlua{%
  luatexbase.remove_from_callback ("process_input_buffer" , 
  "InlineMathYellow" )}}
\AtBeginDocument{\InlineMathColorOn} % activate by default

\begin{document}
$1+1+1\ne2$, 
$1$, 
\(a^2+b^2=c^2\), 
\(\$12.34\), 
\$12.34
$$a^2+b^2=c^2$$ % not inline math
\end{document}
Mico
  • 506,678
  • 1
    ...and now all inline math is unbreakable. – Henri Menke Nov 11 '19 at 21:58
  • @HenriMenke - If I understood the OP's objective correctly, it's to have a quick & dirty method for searching the entire document for instances of inline math -- and to make sure that none of these instances are wider than ca 0.5\textwidth. If my understanding is correct, it shouldn't matter much that all inline math constructs are made unbreakable. Once the OP has fixed (or re-written) the inline formulas that need fixing, all he/she has to do is delete or comment out the instruction \AtBeginDocument{\InlineMathColorOn} and compile the document one more time. :-) – Mico Nov 11 '19 at 22:22
  • @HenriMenke - If highlighting all instances of inline math has to be a permanent feature of the document (the mind shudders), I'd definitely replace \colorbox with some other macro or environment that allows line-breaking of long-ish inline math terms. :-) – Mico Nov 11 '19 at 22:27