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.
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.
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?
% !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}
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
\colorbox with some other macro or environment that allows line-breaking of long-ish inline math terms. :-)
– Mico
Nov 11 '19 at 22:27
$...$or with\(...\)? – Mico Nov 11 '19 at 15:56$...$, but switching to\(...\)is possible (howeever requires a big amount of work) – porton Nov 11 '19 at 16:52$...$and\(...\). – Mico Nov 11 '19 at 19:01