File: mathtex.html

package info (click to toggle)
mathtex 1.03-1
• area: main
• in suites: buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
• size: 540 kB
• ctags: 301
• sloc: ansic: 3,863; makefile: 50
 file content (2545 lines) | stat: -rw-r--r-- 131,004 bytes parent folder | download
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545 mathTeX manual

m a t h T e X   m a n u a l
( for mathTeX version 1.03 )

more_examples...

email: john@forkosh.com

This manual contains more information than you'll probably need to read. If you follow the QuickStart instructions below, try installing mathTeX immediately. If you need more information, continue reading until you feel comfortable trying to install mathTeX. Return to the manual as needed. Prerequisites are: some knowledge of Unix shell, of installing cgi's, of LaTeX.
"Computers are like Old Testament gods: lots of rules and no mercy."
Joseph Campbell, The Power of Myth   (Doubleday 1988, page 18)

C o n t e n t s           Q u i c k S t a r t
(1) Introduction
dependencies
plugins
alternatives
web services
(2) LaTeX markup
input form
examples
(3) MathTeX markup
extra directives
(4) Installation
error messages
compile switches
run from shell

Related   Pages

LaTeX Tutorial
mathTeX changeLog

Installation:     Note: The current release of mathTeX only
runs under Unix-like operating systems.

First, install mathTeX's dependencies:
a recent TeX distribution with dvipng, on
your server. Or see mimeTeX if you can't.
unzip mathtex.zip
cc mathtex.c   \
–DLATEX=\"$(which latex)\" \ –DDVIPNG=\"$(which dvipng)\"   \
–o mathtex.cgi

Finally, just
mv mathtex.cgi to your cgi-bin/ directory,
chmod permissions as necessary,
and you're all done.

Usage:     To see the image

in your html page, just write the tag
<img src="/cgi-bin/mathtex.cgi?
x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}">

(1) Introduction

MathTeX, licensed under the gpl, is a cgi program that lets you easily embed LaTeX math in your own html pages, blogs, wikis, etc. It parses a LaTeX math expression and immediately emits the corresponding gif (or png) image, rather than the usual TeX dvi. So just place an html <img> tag in your document wherever you want to see the corresponding LaTeX expression. For example,

<img src="/cgi-bin/mathtex.cgi?f(x)=\int_{-\infty}^xe^{-t^2}dt"     alt="" border=0 align="middle">

immediately generates the corresponding gif, displaying wherever you put that <img> tag.

mathTeX dependencies...

MathTeX's uses the latex and dvipng programs, along with all necessary fonts, etc, from your TeX distribution. Occasionally, you may need to download dvipng separately.
If you can't, or don't want to, install dvipng, then you may optionally specify the –DDVIPS and –DCONVERT switches when compiling mathTeX. Then mathTeX uses dvips from your TeX distribution, and convert from the ImageMagick package, instead of dvipng.

That is,

cc –DLATEX=\"path/to/latex\" DDVIPNG=\"path/to/dvipng\" mathtex.c –o mathtex.cgi

compiles mathtex.cgi with dependencies latex and dvipng, whereas,

cc –DLATEX=\"path/to/latex\" DDVIPS=\"path/to/dvips\" \    DCONVERT=\"path/to/convert\" mathtex.c –o mathtex.cgi

compiles mathtex.cgi with dependencies latex and dvips and convert.
(Note: dvipng is easily twice as fast as dvips/convert, and it produces somewhat smaller image files, with no discernible (to me) loss of quality or other downside. So I recommend its use if you have it installed or can install it.)

These dependencies — always latex and either dvipng or dvips/convert — must all be installed on your server before you can run mathTeX. Ask your ISP or sysadmin if you have any questions or problems installing them. Or see mimeTeX if you can't install them.

If you're using dvips and and convert (rather than dvipng), then be advised that recent versions of convert seem to exhibit a minor bug whereby the program's –gamma correction option is ignored when converting postscript images to any other format. The default, and unchangeable, gamma renders acceptable-looking png images, but unacceptably light gif images. Earlier versions of convert seem to respect the –gamma option and, moreover, render acceptable default gif images anyway. So if you're using dvips/convert and are seeing very light images, I'd recommend downloading ImageMagick version 6.2.6 source from http://sourceforge.net/projects/imagemagick/files/ and building your own version of convert. That should fix your problem. Alternatively, an even easier fix is to compile mathtex with the -DPNG switch, which renders acceptable-looking default png images regardless of convert version.

mathTeX plugins...

There's no inherent need to repeatedly write the cumbersome <img> tag illustrated above. You can write your own custom tags, or write a wrapper script around mathTeX to simplify the notation.

For example, the following javascript snippet (based on mathtran's mathtran_img.js) lets you just write   <img alt="mathtex:c=\sqrt{a^2+b^2}">   wherever you want to see

<script type="text/javascript">    <!--    // Create a namespace to hold variables and functions    mathtex = new Object();    // Change this to use your server    mathtex.imgSrc = "http://www.yourdomain.com/cgi-bin/mathtex.cgi?";    // Transform the whole document: add src to each img with    // alt text starting with "mathtex:", unless img already has a src.    mathtex.init = function () {        if (! document.getElementsByTagName) return;        var objs = document.getElementsByTagName("img");        var len  = objs.length;        for (i=0; i<len; i++) {           var img = objs[i];           if (img.alt.substring(0,8) == 'mathtex:')              if (!img.src) {                 var tex_src = img.alt.substring(8);                 img.src = mathtex.imgSrc + encodeURIComponent(tex_src);                 // Append TEX to the class of the IMG.                 img.className +=' tex'; }           }        mathtex.hideElementById("mathtex.error"); }    // Utility function    mathtex.hideElementById = function (id) {        var obj = document.getElementById(id);        if (obj) obj.style.display = 'none'; }    // resolve a cross-browser issue (see CBS events)    mathtex.addEvent = function (obj, evType, fn, useCapture) {        if (obj.addEventListener) { //For Mozilla.            obj.addEventListener(evType, fn, useCapture);            return true; }        else if (obj.attachEvent) { //For Internet Explorer.            var r = obj.attachEvent("on"+evType, fn);            return r; }        }    // Initialize after entire document is loaded    mathtex.addEvent(window, 'load', mathtex.init, false);    -->    </script>

Bulletin boards, wikis, etc, can also incorporate mathTeX images with short scripts. For example, if you're using phpBB2, then Jameson contributed the following one-line mod that lets you write $$...$$ for mathTeX images:

#--------[open]-----------------------------------------------------      /includes/bbcode.php    #--------[find]-----------------------------------------------------      // Remove our padding from the string..    #--------[before, add]----------------------------------------------      $text = preg_replace('/$tex$(.*?)$\/tex$/ie', "'<img src=\"/cgi-bin/mathtex.cgi?'.rawurlencode('$1').'\" align=\"middle\" />'",      text); Similarly, if you're using phpBB3, then no mod is even needed. Just click Postings from the Administrator Control Panel, and add the Custom BBCode $${TEXT}$$ with the HTML replacement <img src="/cgi-bin/mathtex.cgi?{TEXT}" align=middle> Now you can also write $$...$$ to obtain mathTeX images of the enclosed expression. Plugins for several additional packages already exist for my other math-rendering program, mimeTeX, which runs without dependencies, but produces slightly lower quality images than LaTeX. These plugins also work with mathTeX. Just substitute mathtex.cgi wherever the instructions say mimetex.cgi. Package Plugin PmWiki mimeTeX plugin MediaWiki "mimeTeX alternative" MathWiki "mimeTeX Parser" PunBB mimeTeX plugin Movable Type mimeTeX plugin WordPress mimeTeX plugin Please note: If you're writing your own plugin for mathTeX, please write code using system( ), or any other shell escape mechanisms, carefully. system( ) raises security issues, either real ones if used carelessly, or just in the minds of system administrators. Either way, I've received emails from people unable to use mathTeX because of unnecessary system( ) calls prohibited by security-conscious sysadmins. MathTeX itself poses minimal risk when used as illustrated above, but you're responsible for any plugin/wrapper script you write around it. mathTeX alternatives... Other math-on-the-web solutions are discussed at www.tug.org/interest.html, and in the tex-faq/LaTex2Html and tex-faq/mathml. Several LaTeX-based solutions similar to mathTeX that you may want to look at are latexrender, mathtran, textogif, and gladTeX. However, if you can't install a TeX distribution on your server, then you may prefer to look at a stand-alone math rendering program like mimeTeX, which has no dependencies whatsoever, but which produces slightly lower quality images than LaTeX. mathTeX web services... If you have trouble installing mathTeX on your own server, a mathTeX web service is currently available. An <img> tag of the form <img src="http://www.forkosh.dreamhost.com/mathtex.cgi?c=\sqrt{a^2+b^2}" alt="" border=0 align=middle> displays wherever you put that <img> tag in your own document. Note that the typical /cgi-bin/mathtex.cgi? has been replaced by http://www.forkosh.dreamhost.com/mathtex.cgi? in this <img> tag, using mathTeX on my server to render your expression for you. For production use, please install mathTeX on your own server. Installing mathTeX on your own server sets up a de facto web service, unless you compile it with the –DREFERER=\"domain\" switch, restricting requests to that specific domain. Until mathML becomes widespread, LaTeX-based web services, like mathTeX or alternatives, may be the best math-on-the-web solutions. If you set one up that's intended for public access, email me its url, and I'll add it to the following list... url of service test http://www.forkosh.dreamhost.com/mathtex.cgi http://www.cyberroadie.org/cgi-bin/mathtex.cgi unavailable http://www.problem-solving.be/cgi-bin/mathtex.cgi http://www.openmaths.org/cgi-bin/mathtex.cgi The "test" column just exercises the link to its left. Thanks to everyone. An alternative mathTeX-based public web service you may also want to consider is Embedding math with replacemath.js, which allows you to embed math in your html pages without any <img> tags at all, just by writing LaTeX math markup between $$...$$ . Just prepare your html document in the form <HTML> <HEAD> <TITLE> test page </TITLE> </HEAD> <BODY> Put any LaTeX math expression you like between $$...$$, for example $$y=\frac1{1-x^2}$$ renders </BODY> <script type="text/javascript" src="http://mathcache.s3.amazonaws.com/replacemath.js"> </script> <script type="text/javascript"> replaceMath( document.body ); </script> </HTML> That is, near the bottom of your document, after the </BODY> tag, place exactly the two <script> tags shown. Then, anywhere in your document, any LaTeX math expressions surrounded by $$...$$ will be rendered for you by their public web service. (2) LaTeX math markup MathTeX uses latex to render images, and you must already be familiar with LaTeX math markup to use it. If you're not, many online LaTeX tutorials are readily available. You may also want to browse Andrew Roberts' Latex Math I and Latex Math II, or my own LaTeX math tutorial. You can also download and read the AMS's Short math guide. LaTeX markup... Now, to try out mathTeX, just enter any expression you like in the Query Box below. I've started you out with a little example already in the box, or you can Click any of the Examples below to place that corresponding expression in the Query Box. Then press the Submit button, and mathTeX's rendering should be displayed in the little window immediately below it. First enter your own LaTeX expression, or Click any example... Now click Submit to see it rendered below... You should see if you submit the sample expression already in the box. Examples... Here are various additional random examples further demonstrating mathTeX's features and usage. To see how they're done, Click any one of them to place its corresponding expression in the Query Box above. Then press Submit to re-render it, or you can edit the expression first to suit your own purposes. (1) (2) definition of derivative (3) illustrating \left\{...\right. and note the accents (4) \overbrace{}^{} and \underbrace{}_{} (TeXbook page 181, Exercise 18.41) (5) \begin{array} (6) using \begin{eqnarray*} to align equations (3) MathTeX markup extensions To facilitate its use in html <img> tags, mathTeX recognizes several special \directives that modify latex's behavior. These \directives are usually interpreted by mathTeX, and then removed from your expression before it's submitted to latex for rendering. For example, • <img src="/cgi-bin/mathtex.cgi?\png x^2"> renders x^2 as a png image instead of the default gif format. • <img src="/cgi-bin/mathtex.cgi?\usepackage{color}\color{blue}x^2"> renders in blue. In this case, mathTeX removes the familiar LaTeX \usepackage directive from your math expression, and places it in the preamble where it belongs. You can have as many special mathTeX \directives in an expression as you like. They're recognized anywhere at all in an expression, though these examples show them at the beginning. To simplify readability, additional examples below are more tersely illustrated as "query-string" rather than as <img src="/cgi-bin/mathtex.cgi?query-string">. mathTeX directives... \displaystyle or \textstyle or \parstyle mathTeX's usual default wraps the ?query-string expression "\int_0^1 f(x)dx" as $\int_0^1 f(x)dx$, rendering it in LaTeX's \displaystyle math mode as . But the expression "\textstyle \int_0^1 f(x)dx" is wrapped as \int_0^1 f(x)dx $, rendering instead. However, if you compiled mathtex.cgi with the -DTEXTSTYLE option, then \textstyle is the default, and you can write \displaystyle in your expressions to override it. Finally, writing \parstyle in an expression leaves it completely unwrapped, and rendered in LaTeX's paragraph mode. When using \parstyle, write your own 's and$ $'s as needed. For example, "\parstyle\noindent the answer is$\frac89$" renders . In this case you could just as easily write a LaTeX \mbox{ } in math mode. But you may find \parstyle useful for expressions using LaTeX's eqnarray environment, or for other purposes. \usepackage{packagename} When mathTeX sees a \usepackage in your expression, it's removed from the expression and placed in the preamble. So, for example, \usepackage{color} \color{blue} x^2+y^2 renders in blue. You can have up to nine \usepackages's in an expression. The preamble of mathTeX's default wrapper script already contains \usepackage[latin1]{inputenc}, \usepackage{amsmath} \usepackage{amsfonts} and \usepackage{amssymb}. \tiny thru \Huge mathTeX moves LaTeX's ten standard size directives outside the math mode wrapper, so that they can have their intended effect. For example, the expression "\Large x^2" is wrapped as \Large $x^2$, rendering . LaTeX's \normalsize default renders . However, if your expression contains mathTeX's \parstyle directive, then any LaTeX size directives are left in place, exactly where you've written them. \dpi{dots-per-inch} mathTeX runs dvipng (or convert) at default screen resolution of 120dpi. The directive \dpi{300} generates a much larger 300dpi image instead. Here are samples of several dpi's rendered at \scriptsize and \normalsize, dpi \scriptsize \normalsize 100 120 160 200 \gammacorrection{gamma-correction} mathTeX runs dvipng with default gamma=2.5 (or convert with default 0.5). The directive \gammacorrection{3.5} renders a darker-than-usual gamma=3.5 image when run with dvipng (or lighter-than-usual when run with convert). Note: This gamma correction confusion arises, I believe, as follows. Consider a grayscale from x=0 for black to x=1 for white. Then, the canonical formula to apply gamma correction is , whereby becomes blacker and becomes whiter. The convert program appears to follow this convention, whereas dvipng applies instead. Here are some samples (unchanging grayscale values up and down a column signal the corresponding program is unavailable) gamma dvipng dvips/convert 0.25 0.50 1.0 2.0 4.0 \gif or \png mathTeX's default renders expressions as gif images. If you want a png image instead, write the directive \png in your expression. However, if you compiled mathtex.cgi with the –DPNG switch, then png is the default, and you can write \gif in your expression to obtain a gif image. \dvips or \dvipng mathTeX uses dvipng when it's available, or dvips and convert otherwise, to render LaTeX's dvi output as gif or png images. If all three programs are available on your server, then expressions containing \dvips are rendered using dvips/convert instead of dvipng. Or, when dvips/convert is the default (see \switches below), expressions containing \dvipng are rendered using dvipng instead of dvips/convert. \quiet or \noquiet or \nquiet{n} If any "! LaTeX Error:" is emitted while processing your expression, mathTeX's default replies <Enter> to the first three errors, and then replies "x", halting LaTeX before it produces a .dvi file. But if your expression contains \quiet, then mathTeX replies "q" to the first error, making LaTeX enter batchmode, whereas if your expression contains \noquiet, then mathTeX replies "x" to the first error, halting LaTeX immediately. If your expression contains \nquiet{n}, then mathTeX replies <Enter> to the first n errors, and then replies "x". \cache or \nocache By default, mathTeX saves each new image in its cache directory, rather than re-rendering the same image every time the same expression is submitted. But expressions containing \nocache are not cached. Expressions containing \today and/or \time are automatically not cached. Otherwise, include \nocache in your expression if it contains volatile information that might affect its appearance between renderings. (The \cache directive forces the image of that expression to be cached, but is usually unnecessary since caching is the default.) \msglevel{verbosity} To help debug problems that matheTeX's error messages don't resolve, resubmit the same failed expression with \msglevel{9} added. After it fails again, login to a shell on your server, and cd cgi-bin/mathtex/ to mathTeX's cache directory. Then ls -alt|less to see the most recent files. These should include yourexpression.out (and yourexpression.gif if an image was created), where filename yourexpression is the 32-character MD5 hash of your failed expression. Now cd .. back up to your cgi-bin/ directory, and you should see a new directory cgi-bin/yourexpression/ created by mathTeX. cd to it, and follow the command-line error instructions below. When done, you should probably rm -r cgi-bin/yourexpression/ which is no longer needed (ditto the .out and .gif files in mathTeX's cache if you want to clean up completely). \which{programname} mathTeX must be compiled with several –Dswitches that specify paths to its dependencies. If you can't determine these required paths, mathTeX's \which{programname} may provide some help (also see \switches below). For example, directive renders \which{latex} \which{dvipng} \which{dvips} \which{convert} displays known paths to mathTeX's dependencies, as they're installed on this server. However, there's a small "Catch-22" (circular logic that may bite you): mathTeX must already know these paths before it runs the programs that display them. You may be able to temporarily circumvent this problem by compiling cc mathtex.c –o mathtex.cgi without any required switches at all. When compiled like this, mathTeX uses which to determine the required paths. If they're found, then mathTeX will run and display them; if not, you'll likely see message 7 or message 9 instead. If mathTeX does run, and displays the required paths, you should immediately re-compile it with the required –Dswitches. Execution time can be doubled, or even worse, when mathTeX has to find these paths itself. When \which{programname} can't find the path, mathTeX displays instead. But mathTeX may also display , signalling that Unix which failed. In this case, mathTeX searches your server's locate database, if it exists, and displays that path instead. The "not found" message signals which and locate both failed. \switches Submitting the expression \switches to mathTeX renders , displaying several (more will be added) of mathTeX's compile-line switches. "Program image" isn't really a switch: it's the filename of the running program, usually mathtex.cgi unless you compiled it differently. "Paths" displays what mathTeX knows about the full paths to its dependencies. On the right-hand side of each path is the source of that information. There are four possible sources, listed in order of reliablility (most reliable first): • (switch) means you compiled mathTeX with a –Dswitch for that path. You should always compile mathTeX with its dependencies: –DLATEX, and either –DDVIPNG or both –DDVIPS and –DCONVERT. When that's not possible, read \which{ } above and temporarily compile mathTeX without them. • (which) means the corresponding –Dswitch was not supplied, but Unix which found the program. If that's latex or another of your dependencies, then re-compile mathTeX with a –Dswitch containing the displayed information. • (locate) means Unix which failed, but your server's locate database found the path. Again, re-compile mathTeX if that's one of your dependencies. • (default) means no path information was found. A default path programmed into mathTeX is displayed instead. This default is frequently wrong. In fact, (default) typically means that the corresponding program isn't installed on your server at all. In that case, if the program is one of your dependencies, you'll likely see message 7 or message 9 instead of the \switches output illustrated above. Either dvipng or dvips and convert can render LaTeX's dvi output as gif or png images. MathTeX's default choice is based on the reliablity of path information to these programs: (switch) is most reliable, followed by (which) or (locate), rated equally, and finally (default) is least realiable. If a (switch) path to dvipng is available (i.e., if you compiled mathTeX with a –DDVIPNG switch), then dvipng is default. Otherwise, if (switch) paths to both dvips and convert are available, then they're default. Otherwise, (which) or (locate) paths are preferred to (default), and dvipng wins a "tie". \time LaTeX's \today is usually sufficient for the slow-paced world of print. But you may want finer-grained resolution for the faster-paced online world. mathTeX provides \time for this purpose, replacing it with the current hh:mm:ss wherever it occurs. The date/timestamp at the top of this document is rendered by mathTeX with an expression like "\parstyle\begin{center}\today\\\time\end{center}". You can see the exact expression by clicking on that image. I'm not reproducing it here because mathTeX doesn't cache images containing \today or \time. So use \time sparingly because every occurrence is re-rendered through LaTeX. \advertisement An expression containing \advertisement is displayed along with your mathTeX advertisement rather than by itself, regardless of -DADFREQUENCY. For example, c=\sqrt{a^2+b^2} \advertisement c=\sqrt{a^2+b^2} See Advertisement to replace the default advertisement, illustrated above, with your own. \version An expression containing \version is displayed along with mathTeX's current version number. For example, \version c=\sqrt{a^2+b^2} \environment Submitting the expression \environment to mathTeX renders displaying the http environment variables known to mathTeX. This is primarily a programming aid, showing information available to mathTeX that might facilitate future enhancements. (4) Installation and Testing Note: The current release of mathTeX only runs on Unix-like operating systems. Very quickly --- First, install mathTeX's dependencies: a recent TeX distribution with dvipng, on your server, or see mimeTeX if you can't. Then, download mathtex.zip and type unzip mathtex.zip cc mathtex.c –DLATEX=\"$(which latex)\"   \
–DDVIPNG=\"$(which dvipng)\" \ –o mathtex.cgi (see –Dswitches below for more information). Finally, mv mathtex.cgi to your cgi-bin/ directory, chmod permissions as necessary, and you're all done. Read the rest of this section only if you want more information. mathTeX's source code is standard Unix C, which should compile and run without change on any posix-compliant Unix platform. The current release of mathTeX only runs under Unix-like operating systems. The three steps needed to compile, install and test mathTeX are: (1) Install LaTeX and download mathTeX... • First, make sure you have a recent TeX distribution with dvipng installed on your server. MathTeX's dependencies are the latex and dvipng programs, along with all necessary fonts, etc, from your TeX distribution. If you prefer to compile mathTeX with the optional –DDVIPS and –DCONVERT switches (Step 2 immediately below), then dvips from your TeX distribution, and convert from the ImageMagick package, are used instead of dvipng. These dependencies — always latex and either dvipng or dvips/convert — must all be installed on your server before you can run mathTeX. Ask your ISP or sysadmin if you have any questions or problems installing them. Or see mimeTeX if you can't install them. • Then download and unzip mathtex.zip in any convenient working directory. Your working directory should now contain README mathTeX release notes COPYING GPL license, version 3, under which you may use mathTeX mathtex.c mathTeX source program and all required functions mathtex.html this file, the mathTeX user manual (2) Compile mathTeX and test it from the Unix shell... • To compile a mathTeX executable that emits default gif images generated by dvipng (recommended), just type cc mathtex.c –DLATEX=\"$(which latex)\"   \
–DDVIPNG=\"$(which dvipng)\" –o mathtex.cgi Alternatively, to compile a mathTeX executable that emits default gif images generated by dvips/convert, just type cc mathtex.c –DLATEX=\"$(which latex)\"   \
–DDVIPS=\"$(which dvips)\" –DCONVERT=\"$(which convert)\"   \
–o   mathtex.cgi
But note that dvipng is easily twice as fast as dvips/convert, and it produces somewhat smaller image files.
For default png (instead of gif) images, add the optional –DPNG switch to either cc command above. All of mathTeX's optional compile-line switches are discussed below.
The required switches shown above define paths to mathTeX's dependencies, either latex and dvipng, or latex and dvips/convert. See required switches below for additional information about these path switches, including discussion of that $(which program) construction. • Immediately after compiling mathTeX, test it from the Unix shell by typing ./mathtex.cgi "x^2+y^2" –m 9 –o test at the command prompt. After the copyright notice, screen output should look something like mathTeX> running image: ./mathtex.cgi mathTeX> input expression: "x^2+y^2" mathTeX> working directory: 8dfaf8281769c217b7e78b27a4747285/ mathTeX> output image file: test.gif Bring up the output file test.gif in your browser, which should display the rendered image of x^2+y^2. If it does, you can rm -r that working directory and proceed to the next (Install) step. If not, there's some error that _must_ be fixed before proceeding: mathTeX is not going to emit gifs from your server if it won't run from the command line. To troubleshoot command-line errors... • First, cd 8dfaf8281769c217b7e78b27a4747285/ to that working directory (whose name is the 32-character MD5 hash of your expression) shown on the screen output above. • If it's not an empty file, view latex.err A message something like latex: command not found probably means that your –DLATEX=\"path/to/latex\" is wrong, or that latex is installed wrong. Fix that, or any other indicated error, and then re-compile mathTeX and test it again. • Otherwise, if latex.err is empty, then view latex.out which contains runtime messages issued by latex, including messages reporting unresolved errors. For example, \usepackage{junk} reports the error File `junk.sty' not found. Find and fix any reported errors, and then re-compile mathTeX and test it again. • If latex.err and latex.out contain no errors, then latex.dvi should contain the dvi image of your expression. Check it with xdvi or any other dvi viewer supplied with your latex distribution. Then view dvipng.err and dvipng.out for any reported errors (note that dvipng emits a benign copyright message to dvipng.out). • You should have found some error along the way. Troubleshoot as necessary. Your most likely (and most obvious) problems are: mathTeX's dependencies are installed incorrectly or not at all, you compiled mathtex.cgi with the wrong paths (check which's output), or you don't have rw permissions in the directory you're testing from. (3) Install mathTeX and test it from a browser... • Install your compiled mathtex.cgi executable only after successfully testing it from the Unix shell. Just mv mathtex.cgi to your web server's cgi-bin/ directory, chmod permissions as necessary, and you're all done. cgi-bin/mathtex.cgi must be executable by your web server, and it must have rw permissions in the cgi-bin/ directory where it's installed. The first time it runs, mathtex.cgi will mkdir mathtex/, where rendered images are subsequently cached. Permissions and ownerships must be set to allow this. chmod 755 typically works, but ask your ISP or sysadmin if you have any questions or problems. • Immediately after installation, type a url into your browser's locator window something like http://www.yourdomain.com/cgi-bin/mathtex.cgi?\message which should display message 1 in the upper-left corner of your window. If, instead, you see Not Found or Error 500 emitted by your server, then mathTeX isn't running. Check that you installed it in the correct directory, set its permissions properly, etc. • The preceding test just checked that mathtex.cgi runs from your server. This current test checks that your –DLATEX, etc, paths are correct, and that mathtex.cgi has rw permissions in your cgi-bin/ directory. Type a url into your browser's locator window something like http://www.yourdomain.com/cgi-bin/mathtex.cgi?x^2+y^2 which should display in the upper-left corner of your window, just like clicking this link does, which tests my mathtex.cgi, http://www.forkosh.com/cgi-bin/mathtex.cgi?x^2+y^2 If you see the same image from your own yourdomain link, then you've completed a successful mathTeX installation. Otherwise, you'll probably see one of mathTeX's error messages illustrated below. Read the accompanying description, and try to resolve the problem accordingly. Run-time error messages... Gif images for 15 messages are embedded in mathTeX, displayable so long as mathtex.cgi can run from your server, even without latex and without rw permissions in your cgi-bin/ directory. In addition, your server may display the first two messages below if mathtex.cgi can't run. Any embedded mathTeX message can be intentionally displayed by submitting an expression containing the special mathTeX directive \message{1} through \message{15} (an out-of-bounds argument, or \message with no argument, displays message 1). Otherwise, various errors signal "unintentional" displays of the corresponding message, e.g., if your –DLATEX switch specifies the wrong path to latex, then you'll see message 7 (unless some earlier error supercedes it). Message Description The requested URL was not found. You typed the wrong url, or mathtex.cgi is not installed where you think it is. Internal server error 500 If mathtex.cgi's permissions are chmod'ed improperly, if your account isn't set up to run cgi's, etc, then mathTeX will not run at all. You'll probably see this error message emitted by your server instead. Immediately after installing mathtex.cgi to your cgi-bin/ directory, type a url of the form yourdomain.com/cgi-bin/mathtex.cgi?\message into your browser. You should see this message. It means mathtex.cgi ran successfully, its permissions are set properly, and the account hosting yourdomain can run cgi's. Traps any otherwise unidentified error condition. The combination of permissions/ownerships on mathtex.cgi itself, and on the cgi-bin/ directory where it's installed, prohibit mathTeX from creating its cache directory mathtex/ underneath cgi-bin/. Change permissions/ownerships as needed. Same problem as message 3 above, except this time mathTeX can't create a temporary work directory under cgi-bin/. Unanticipated error. MathTeX should be able to cd to a directory it just created. Unanticipated error. MathTeX should be able to open a file (for write) in a directory it just created. Either latex is not installed, or your –DLATEX path to it is incorrect. It's also possible that the shell host on which you compiled mathTeX has different volumes or mount points than your server (see path switches). Check with your ISP or sysadmin, or try mathTeX's \which directive. A simple latex error, like \alfa instead of \alpha, should not cause this problem (unless your expression contains \noquiet). It's more likely caused by a missing font or package, etc. Simplify your expression until it works, and see if that helps identify the cause. Or add \msglevel{9} to your expression, and check files latex.out and latex.err for error messages. Either dvipng is not installed, or your –DDVIPNG path to it is incorrect. Also see the remark in message 7. Rerun the same expression with \msglevel{9} added. Then check files latex.out and latex.err, and dvipng.out and dvipng.err for any clues to the cause of this error. Either dvips is not installed, or your –DDVIPS path to it is incorrect. Also see the remark in message 7. Rerun the same expression with \msglevel{9} added. Then check files latex.out and latex.err, and dvips.out and dvips.err for any clues to the cause of this error. Either convert is not installed, or your –DCONVERT path to it is incorrect. Also see the remark in message 7. Rerun the same expression with \msglevel{9} added. Then check files latex.out and latex.err, dvips.out and dvips.err, and convert.out and convert.err for any clues to the cause of this error. An image file was apparently created successfully, but is now inaccessible to mathTeX. Rerun the same expression. If it fails again, rerun it with \msglevel{9} added. Then check all .out and .err files for any clues to the cause of this error. Compile-line switches... Required switches... MathTeX's required –D switches specify paths to the programs it needs to render LaTeX expressions as images. Your Unix PATH environment variable usually contains the directories where these programs reside. And in that case, the Unix shell command which progname emits the string /path/to/progname. Then you can manually copy which's output to the switch, e.g., if which latex emits /usr/bin/latex then just write the switch –DLATEX=\"/usr/bin/latex\" Alternatively, you can use the Unix shell's$( )   construction to automatically pipe which's output into the switch, e.g.,   –DLATEX=\"$(which latex)\" automatically places that same path to latex between the literal \" \" quotes. If which doesn't work, you must nevertheless make sure that latex and mathTeX's other dependencies are all installed on your server. Then determine the proper paths to them yourself (ask your ISP or sysadmin, or try mathTeX's \which directive), and manually write mathTeX's required –D switches as described above. Occasionally, which may seem to work, but actually doesn't, because your shell account and internet server are hosted on different machines, with different volumes mounted and/or different mount points. When this happens, server volumes are nfs-mounted by your shell machine, so you can work on your internet files. Conversely, shell volumes aren't necessarily mounted by the server, so latex could be visible from your shell but not from the server. Check with your ISP or sysadmin about network topology if you suspect something like this, or mathTeX's \which directive may help. In any case, mathTeX's dependencies, latex and either dvipng or dvips/convert, must be available to your server, and you must compile mathtex.cgi with their paths on your server. –DLATEX=\"/path/to/latex\" –DDVIPNG=\"/ path/to/dvipng\" mathTeX always requires the –DLATEX switch, and its recommended default (using dvipng to render latex's dvi output as gif or png images) also requires the –DDVIPNG switch. So your standard cc command to compile mathTeX looks like cc mathtex.c \ –DLATEX=\"$(which latex)\"   \
–DDVIPNG=\"$(which dvipng)\" \ –o mathtex.cgi –DDVIPS=\"/path/to/dvips\" –DCONVERT=\"/ path/to/convert\" If you can't (or don't want to) use dvipng, then compile mathTeX with the –DDVIPS and –DCONVERT switches (instead of –DDVIPNG). Then dvips from your TeX distribution, and convert from the ImageMagick package, are used (instead of dvipng) to render latex's dvi output as gif or png images. In this case, your cc command to compile mathTeX looks like cc mathtex.c \ –DLATEX=\"$(which latex)\"   \
–DDVIPS=\"$(which dvips)\" \ –DCONVERT=\"$(which convert)\"   \
–o   mathtex.cgi
Finally, if all three programs (dvipng and dvips and convert) are installed on your server, you can compile mathTeX with all the –D switches. This defaults to dvipng, but permits users to submit expressions containing the special \dvips and \dvipng directives, regardless of mathTeX's default. So a comprehensive cc command looks like
cc   mathtex.c   \
–DLATEX=\"$(which latex)\" \ –DDVIPNG=\"$(which dvipng)\"   \
–DDVIPS=\"$(which dvips)\" \ –DCONVERT=\"$(which convert)\"   \
–o   mathtex.cgi
along with any other optional switches you choose from those described below,
Optional switches...
In addition to the –DLATEX and –DDVIPNG switches required on the mathTeX's compile line, as discussed above, you may also include the following optional –D switches, whose functionality is discussed below. Whenever a switch takes a value, its default value is illustrated. An italicized value means there is no default.
–DCACHE=\"mathtex/\"
By default, mathTeX saves each new image it renders to a file in directory   mathtex/   (relative to the cgi-bin/ directory where you installed mathtex.cgi). Then, every time it's given the same expression, mathTeX reads that file rather than re-rendering the same image. You can specify any other cache directory with the   –DCACHE=\"path/\"   switch. Either way, mathTeX's cache directory must be read/writable by it, so set permissions (typically chmod 755) as necessary.
mathTeX occasionally disables caching, e.g., expressions containing \today are always re-rendered since the date may have changed. Otherwise, caching is mandatory and cannot be disabled.
Cached image files are named filename.gif or filename.png, where filename is the 32-character MD5 hash of the LaTeX expression. When caching a new image, mathTeX also updates the file path/mathtex.log containing a timestamp, filename, LaTeX expression, and http referer for each new file created. A sample entry looks like
--------------------------------------------------------------------- 2007-10-11:09:00:53am            f8ccc8dd93c8eeb1d9c40b353ef781e0.gif \LARGE x=\frac{-b\pm\sqrt{b^2-4ac}}{2a} http://www.forkosh.com/mathtex.html ---------------------------------------------------------------------
–DGIF   or   –DPNG
mathTeX generates gif or png images. Users can specify which format they want by including a \gif or \png mathTeX directive in submitted expressions. Otherwise, mathTeX's default is specified at compile time by a   –DGIF   or   –DPNG   switch. If a submitted expression contains neither directive, and if mathTeX was compiled with neither switch, then gif is the default.
–DDISPLAYSTYLE   or   –DTEXTSTYLE   or   –DPARSTYLE
mathTeX's default wraps user expressions inside   ,   rendering generated images in LaTeX's \displaystyle math mode. The   –DTEXTSTYLE   switch wraps expressions inside   ,   rendering generated images in LaTeX's \textstyle math mode. And the   –DPARSTYLE   switch leaves expressions unwrapped, rendering generated images in LaTeX's default paragraph mode. Users can override any compiled default with a mathTeX style directive.
–DFONTSIZE=5
You can specify   –DFONTSIZE=1   thru   –DFONTSIZE=10   on mathTeX's compile line, corresponding to default LaTeX font sizes \tiny thru \Huge. If no switch is supplied, the default 5 corresponds to \normalsize. Users can override any compiled default with a mathTeX \tiny...\Huge directive.
–DDPI=\"120\"
The   –DPI   switch changes mathTeX's default screen resolution of \"120\" dots-per-inch, which you enter as a string. Some samples are illustrated above.
–DGAMMA=\"2.5\"
The   –DGAMMA   switch changes mathTeX's default gamma correction of \"2.5\" (for dvipng), which you enter as a string. Some samples are illustrated above.
–DQUIET     or
–DNOQUIET     or
–DNQUIET=n
If any "! LaTeX Error:" is emitted while processing your expression, mathTeX's default replies <Enter> to the first three errors, and then replies "x", halting LaTeX before it produces a .dvi file. Compiling mathTeX with   –DQUIET   replies "q" to the first error, making LaTeX enter batchmode, whereas compiling with   –DNOQUIET   replies "x" to the first error, halting LaTeX immediately. Compiling mathTeX with   –DNQUIET=n   replies <Enter> to the first n errors, and then replies "x". User expressions may contain \quiet\noquiet or \nquiet{n} to override any compiled default.
–DREFERER=\"domain\"     or
–DREFERER=\"domain1,domain2,etc\"
If you compile mathTeX without a   –DREFERER=\" \"   switch, then anyone on the internet can use your mathtex.cgi program by writing a url of the form   http://www.yourdomain.com/cgi-bin/mathtex.cgi?x^2+y^2.   So you're essentially providing mathTeX as a free web service. I encourage this if you have the compute and disk resources to spare.
Otherwise, if compiled with   –DREFERER=\"domain\",   then mathTeX performs a case-insensitive search of the environment variable HTTP_REFERER to verify that it contains the authorized   domain   as a substring.
Or, if given a comma-separated list containing several   domain's   (second form of the switch), then HTTP_REFERER must contain either domain1 or domain2, or etc, as a (case-insensitive) substring.
If HTTP_REFERER doesn't contain a substring matching any of these domain(s), then mathTeX emits the error message image (reporting HTTP_REFERER on the first line)

–DKILLTIME=10
Some expressions can unintentionally (or not) force LaTeX to loop endlessly, causing mathTeX to hang. To avoid this problem, if LaTeX fails to complete within KILLTIME seconds, mathTeX kills it and emits an error. The built-in code for this purpose is in mathTeX's timelimit( ) function, which was adapted from timelimit-1.4. Compiling mathTeX with   –KILLTIME=0   places no timelimit restrictions on LaTeX whatsoever; probably not a good idea.
–DTIMELIMIT=\"/path/to/timelimit\"
"\\atop \\mbox{\\scriptsize http://www.forkosh.com/mathtex.html}$}\\\\ \n" "\\vspace*{-4mm}\n" " %%beginmath%% %%expression%% %%endmath%% \n" "\\end{center}\n" Use the same editing rules as –DUSEPACKAGE and –DNEWCOMMAND above: every line in the file is enclosed in "quotes", contains a \n before the closing quote, and all other backslashes are written as double-backslashes \\. Note \\\\ at the end of the third line, which LaTeX sees as \\. The entire example shows how my default advertisement is defined. Your advertisement may consist of any valid LaTeX commands you like. But it must somewhere contain the line " %%beginmath%% %%expression%% %%endmath%% \n" which is replaced by the user's expression, surrounded by whatever math mode delimiters it specifies. The document remains in paragraph mode, allowing$ \$   and      to be placed wherever you like.

Running mathTeX from the shell...

MathTeX is usually run by your web server as a cgi program, obtaining its input expression from the query-string of an html <img> tag. But you can also run mathTeX from your Unix shell, supplying all input on the command line. For example,   ./mathtex.cgi "x^2+y^2" –o equation1   renders an image of x^2+y^2 in file equation1.gif. And with the   –m 9   switch, it's also useful for testing.

The complete command-line syntax for mathTeX is

"My grandfather once told me there are two kinds of people:
Those who do the work and those who take the credit.
He told me to try to be in the first group; there was much less competition.
"
Indira Gandhi, the late Prime Minister of India

MathTeX's copyright is registered by me with the US Copyright Office, and I hereby license it to you under the terms and conditions of the GPL. There is no official support of any kind whatsoever, and you use mathTeX entirely at your own risk, with no guarantee of any kind, in particular with no warranty of merchantability.

By using mathTeX, you warrant that you have read, understood and agreed to these terms and conditions, and that you possess the legal right and ability to enter into this agreement and to use mathTeX in accordance with it.

Hopefully, the law and ethics regarding computer programs will evolve to make this kind of obnoxious banter unnecessary. In the meantime, please forgive me my paranoia.

To protect your own intellectual property, I recommend Copyright Basics from The Library of Congress, in particular Circular 61, Copyright Registration for Computer Programs. Very briefly, download Form TX and follow the included instructions. In principle, you automatically own the copyright to anything you write the moment it's on paper. In practice, if the matter comes under dispute, the courts look _very_ favorably on you for demonstrating your intent by registering the copyright. For example, courts will stop unauthorized use of unregistered material, but monetary damages are awarded _only_ if you register the copyright before infringement occurs.

Concluding Remarks

I hope you find mathTeX useful. If so, a contribution to your country's TeX Users Group, or to the GNU project, is suggested, especially if you're a company that's currently profitable.