How can I write a Template.pm filter to generate PNG output from LaTeX source code? - perl

I am looking for a way of generating PNG images of equations from LATEX source code embedded in templates. For example, given:
[% FILTER latex_display ]
\begin{eqnarray*}
\max && U(x,y) \\
\mathrm{s.t.} && p_x x + p_y y \leq I \\
&& x \geq 0, y \geq 0
\end{eqnarray*}
[% END %]
I would like to get the output:
<div class="latex display"><img src="equation.png" width="x" height="y"></div>
which should ultimately display as:
I am using ttree to generate documents offline. I know about Template::Plugin::Latex but that is geared towards producing actual documents out of LATEX templates.
Any suggestions?
Update:
I appreciate the suggestions (and I have voted them up). However, they are no quite what I need. I would really appreciate a few pointers towards getting started with a Plugin or Filter for Template Toolkit.

Wikipedia does something similar to what you want. They use a program called texcv. Then you also have MathJAX which is more aimed at dynamic math rendering in web-pages.
texcv is according to the linked page written in OCaml however since the rest of Mediawiki is written in PHP I don't think you should have any problem with repurposing the program to use with Perl.

you should take a look at jsMath, MathML, or mathjax

I would really appreciate a few pointers towards getting started with a Plugin or Filter for Template Toolkit
These docs on Template Toolkit site should provide all necessary info to setup a plugin or filter:
Plugin provider module
Base class for plugin filters
Once you have your LATEX to PNG conversion sorted then you probably need to save it as a file using redirect. Example from POD:
[% PROCESS my/png/generator
FILTER redirect("images/logo.png", binmode=1) %]
Hope that helps.
/I3az/

Related

LaTeX math in github wikis

Is it possible to include LaTeX-style math in any way with github repo wikis? Googling implies github no longer allows things like MathJax, but most references are years old. What (if any) alternatives are there to including LaTeX-formatted math in github wikis?
You can use chart.apis.google.com to render LaTeX formulas as PNG.
It work nicely with Githhub's markdown:
Example (Markdown):
The ratio of the momentum to the velocity is
the relativistic mass, m.
![f1]
And the relativistic mass and the relativistic
kinetic energy are related by the formula:
![f2]
Einstein wanted to omit the unnatural second term
on the right-hand side, whose only purpose is
to make the energy at rest zero, and to declare
that the particle has a total energy, which obeys:
![f3] which is a sum of the rest energy ![f4]
and the kinetic energy.
[f1]: http://chart.apis.google.com/chart?cht=tx&chl=m=\frac{m_0}{\sqrt{1-{\frac{v^2}{c^2}}}}
[f2]: http://chart.apis.google.com/chart?cht=tx&chl=E_k=mc^2-m_0c^2
[f3]: http://chart.apis.google.com/chart?cht=tx&chl=E=mc^2
[f4]: http://chart.apis.google.com/chart?cht=tx&chl=m_0c^2
https
Some installations of Github Enterprise reject http and work only if you use https
Rendered:
For simple formulas (such as exponents etc) you may one just to use the available render languages. For example, using Textile, you can do:
_E = mc ^2^_
Thiw will be rendered as:
_ is used for italic style and ^ for superscript.
You can do the same thing in Markdown adding some HTML:
*E = mc<sup>2</sup>*
You can see it in action in this very place:
E = mc2
If you're looking for support for complex math formulas, then you have no better option than using a third-party service generating images for you. mathUrl looks interesting.
As input we give it E = mc ^ 2 and it generates the following link:
http://mathurl.com/render.cgi?E%20%3D%20mc%20%5E%202%5Cnocache
There is good solution for your problem - use TeXify github plugin - more details about this plugin and explanation why this is good approach you can find in that answer.
GitLab Wiki and markdown supports formulas. I moved multiple repos for this reason.
Now GitHub officially supports showing Mathjax in the wiki!
In Markdown, just use $ as the deliminator of inline formulas, and $$ as the deliminator of display formulas.
To add math equations to a GitHub wiki, I used mathURL as suggested by Ionică. It will render your LaTeX equations. Append .png to the generated url and use that url as an image (either block or inline) in your markdown.

Catalyst::View::Wkhtmltopdf creating error with template

I am using Catalyst::View::Wkhtmltopdf but issues are araising,
got the error
Caught exception in wealthe::View::Wkhtmltopdf->process "Void-input at /usr/local/share/perl5/Catalyst/View/Wkhtmltopdf.pm line 98."
When checked found its some issue with template, I have added the following config in myapp.pm
'View::Wkhtmltopdf' => { command => '/usr/local/bin/wkhtmltopdf',
tmpdir => '/usr/tmp',
tt_view => 'TT',
}
Template name is TT.pm under View and TT.pm is rendering correctly,
I have made Wkhtmltopdf.pm file under View with the following contents
package myapp::View::Wkhtmltopdf;
use Moose;
extends qw/Catalyst::View::Wkhtmltopdf/;
PACKAGE->meta->make_immutable();
is there anything else I need to get it working.
My experience with the PDF writing perl modules on CPAN is that every option is either:
extremely simple, but capable of only very rudimentary output
fully featured and capable of professional PDF output, but hideously complex and requiring pretty deep knowledge of PDF internals
We gave up.
Instead of trying to generate PDFs with perl natively from scratch, we attacked the problem by building the pages we wanted to render as PDF with Template Toolkit (in most cases reusing existing templates with different wrappers), and used the excellent wkhtmltopdf to handle the conversion to PDF.
Since then, Catalyst::View::Wkhtmltopdf has become available that makes this even simpler.
One of the easiest way is to create a (quite simple) HTML page then convert it to PDF: https://metacpan.org/pod/PDF::FromHTML
It needs the least amount of PDF knowledge.
The other module could create PDF on the fly but they are much more complicated: https://metacpan.org/pod/PDF::API2

MATLAB: force doc command to open a specified reference HTML page

Say I've written a class in a package, called mypackage.myclass. I've written my own HTML documentation for the package and the class, and have included this within the MATLAB help browser as described in the MATLAB documentation.
I can display this HTML documentation by using the help browser to navigate directly to it, but typing doc mypackage.myclass does not display it; instead it displays some HTML documentation that is auto-generated by helpwin (which is a nice feature, but not what I want - the auto-generated documentation is too techy for my users).
How can I force doc to display my documentation, rather than the auto-generated documentation?
Equivalently:
When you run doc docTopic, inside the doc command the Java class com.mathworks.mlservices.MLHelpServices.showReferencePage(docTopic) gets called. If a reference page for docTopic exists, it displays it and returns a success value. If a reference page doesn't exist, it returns a failure value, which then causes helpwin(docTopic) to get called. Somewhere there must be some catalog that connects values of docTopic with individual reference HTML files. How can I fiddle with that catalog - or can I create one for my package?
MathWorkers and #Yair, please give me enough undocumented rope to hang myself with :)
As far as I know this is not possible and not intended by MathWorks. I don't know of an undocumented way of doing this either. As far as I remember the keywords for doc are hard-coded somewhere.
Depending on your setup you can try the following: Prepare your own doc command that uses web(..., '-helpbrowser') to display HTML pages in MATLAB's help browser:
function doc(topic)
my_topics = {
'foo', 'foo.html'
'bar', 'bar/help/intro.html'
};
for i = 1 : size(my_topics, 1)
if strcmpi(topic, my_topics{i, 1})
web(my_topics{i, 2}, '-helpbrowser');
return;
end
end
% Fall back to MATLAB's doc. Note that our doc shadows MATLAB's doc.
docs = which('doc', '-all');
old_dir = cd();
c = onCleanup(#() cd(old_dir));
cd(fileparts(docs{2}));
doc(topic);
end
If you put that function in a file doc.m and put the corresponding directory at the beginning of the MATLAB path (see help addpath) then it will be called instead of the built-in doc.
Of course you could use some other place to store your custom doc mapping (a file, for instance) or use some kind of dynamic lookup scheme.
UPDATE: As of MATLAB R2012b, the '-helpbrowser' option of web is undocumented. This is probably related to the GUI changes in that MATLAB version, which also include the help browser. web(..., '-helpbrowser') still works as intended, but that may change in future versions of MATLAB. As far as I know, there is no documented way of opening any HTML page in the help browser in R2012b.

cfscript Code Assist in CFBuilder

I'm increasingly using cfscript, and like it where appropriately used.
One problem is that there doesn't appear to be any code assist for cfscript in CF Builder, so I find myself writing the tag of a function to leverage the code Assist, then converting to cfscript (which is silly).
For example:
addParam() is the cfscript equivalent of <cfqueryparam >. I get code assist when writing the the tag version, but not the script equivalent.
Does anyone know if there is a code assist library available for cfscript in cfBuilder? Or is this just a downside of working with cfscript?
Many Thanks in advance!
Jason
Your example is not using native CFScript, it's using the hack-solution Adobe provided for some shortcomings of CFScript's coverage of CF tags, which are implemented as a bunch of CFCs in the custom tags dir of your install. This stuff is not representative of CFML & its CFScript support as a whole.
I find that CFB gives hinting for most native functionality... is this not the case for you? What if you try listAppend() for example? Do you get code-assist for that?
UPDATE
I wonder if you get a warning in CFB on your line equivalent to this:
o = new Query();
? I do, by default. I have to make a link to the CustomTags/com dir, and then use this syntax:
o = new com.adobe.Query();
Then I don't get a warning, and indeed I get the code assist you're expecting. I cannot get it to give me hinting on just the non-qualified path to Query.cfc though.
Not ideal. Or maybe I'm missing something, too.

BIRT: Getting started with the Chart API

So I need a few simple pointers with the BIRT chart API.
I am trying to draw a simple line chart of a numeric value over time. I have a vector of (Date,Double) pairs as input. I have read through the examples in org.eclipse.birt.chart.examples but not yet gained enough traction with the API to start making use of it. Just composing a org.eclipse.birt.chart.model.impl.ChartWithAxesImpl has me stumped.
The eventual destination is an Eclipse editor, and that I can do. Is there a tutorial or book anyone can recommend that might get me going with the BIRT chart side?
I should be very grateful
M.
Well. There is this example: http://www.eclipse.org/articles/article.php?file=Article-BIRTChartEngine/index.html
But I am having some problems actually getting it to work myself. All I end up with is an exception like this:
java.lang.NullPointerException
at org.eclipse.birt.chart.device.swt.SwtTextMetrics.getHeight(SwtTextMetrics.java:178)
at org.eclipse.birt.chart.computation.Methods.computeFontHeight(Methods.java:1272)
at org.eclipse.birt.chart.computation.BIRTChartComputation.computeFontHeight(BIRTChartComputation.java:30)
at org.eclipse.birt.chart.computation.EllipsisHelper$SimpleTester.<init>(EllipsisHelper.java:213)
at org.eclipse.birt.chart.computation.EllipsisHelper.createSimpleTester(EllipsisHelper.java:176)
at org.eclipse.birt.chart.computation.EllipsisHelper.simpleInstance(EllipsisHelper.java:183)
at org.eclipse.birt.chart.computation.LabelLimiter.limitLabelSize(LabelLimiter.java:256)
at org.eclipse.birt.chart.computation.LabelLimiter.limitLabelSize(LabelLimiter.java:211)
at org.eclipse.birt.chart.model.layout.impl.TitleBlockImpl.computeBox(TitleBlockImpl.java:288)
at org.eclipse.birt.chart.model.layout.impl.LabelBlockImpl.getPreferredSize(LabelBlockImpl.java:266)
at org.eclipse.birt.chart.internal.layout.LayoutManager$ChartLayout.<init>(LayoutManager.java:111)
at org.eclipse.birt.chart.internal.layout.LayoutManager.doLayout_tmp(LayoutManager.java:1145)
at org.eclipse.birt.chart.internal.layout.LayoutManager.doLayout(LayoutManager.java:1310)
at org.eclipse.birt.chart.factory.Generator.build(Generator.java:1092)
I had the same trouble with the examples from org.eclipse.birt.chart.examples. There are some good java code snippets, but you do not see to what they correspond. This is why I decided to generate all the examples and put them on a web site: BIRT Charts Gallery.