TeX - LaTeX Asked by Slackow on April 2, 2021
I’m trying to find a tool to convert TeX into something like the following:
{frac{-x}{y}}
to
((-x)/(y))
or
{frac{1}{xln(b)}}
to
((1)/(xln(b)))
I built a very simple converter for it but it has trouble with some edge cases, and I was wondering if there were any tools that already did this.
If you're free to use LuaLaTeX, the following solution may be of interest to you. It sets up a LaTeX macro called TexToText
which calls a Lua function called tex2text
to do most of the work. Four separate steps are performed by tex2text
:
frac
notation to inline-math notation
(backslash) character in front of other TeX macro names (e.g, exp
and ln
)% !TEX TS-program = lualatex
documentclass{article}
usepackage{luacode} % for 'luacode' env. and 'luastringN' macro
begin{luacode}
-- Aux. function to trim first and last char. of a string:
function trim ( s )
return s:sub(2,-2)
end
-- Aux. function to get rid of frac wrapper
function frac2text ( s )
s = s:gsub ( "frac%s-(%b{})%s-(%b{})",
function (u,v)
return "("..trim(u)..")/("..trim(v)..")"
end )
return s
end
-- The main Lua function:
function tex2text ( s )
-- Call frac2text function:
s = frac2text ( s )
-- Drop fence-sizing macros:
s = s:gsub ( "left" , "" )
s = s:gsub ( "right" , "" )
s = s:gsub ( "[Bb]igg?[lmr]?" , "" )
-- Remove leading backslash char from all other macros:
s = s:gsub ( "(%a+)" , "%1ignorespacesafterend" )
-- Encase the result in a pair of parentheses:
return "(" .. s .. ")"
end
end{luacode}
newcommandTexToText[1]{directlua{tex.sprint(tex2text(luastringN{#1}))}}
begin{document}
TexToText{ln(exp(0))} quad
TexToText{frac{-x}{y}} quad
TexToText{frac{1}{xln(b)}}
end{document}
Answered by Mico on April 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP