Extension talk:Wiki2LaTeX

Math tags added
I needed to convert the $$$$ tag, to use formulas on exporting latex documents, so I added some very simple code:

in w2lConfig.php I added the line

$w2l_tags['math']        = array('w2l_mwext_support', "math");

and, in w2LatexUtil.php, into the w2l_mwext_support class I added the function function math($input, $argv, &$parser, $mode = 'wiki') { $output = "\n\begin{math}\n"; $output .= trim($input)."\n"; $output .= "\end{math}\n"; return $output; } just copied and modified from the "pre" function

and it worked! Hope it can be useful for others. :)

(and hope of being in the correct place to post my hints...)
 * I added your code to the corefiles, so the next version will support $$$$ natively. Thanks! HG 16:41, 20 August 2007 (UTC)

Image Processing
I have implemented a simple solution for processing of internal images. It searches for the filename in the images/ directory and copies it to an image directory under the tmp/tmp-123... directory. You can add my little helper class with the following steps:

First of all you need to add a few lines to function internalLinkHelper in w2lParser.php before the line // First, check for |: if ( (stripos($link, "Bild:") === 0) or (stripos($link, "Image:") === 0) ) { $link = str_replace('Bild:', '', $link); $link = str_replace('Image:', '', $link); return "". $link. ""; }

Then you need to include my class file at the top of w2lExporter.php: require_once('w2lImages.php');

In w2lExporter.php you need to edit function w2l_unknown_action</tt> to process the images. I added the following line $parsed = w2lImages::processImages($parsed, $mytemp); to the sections where $action</tt> is w2lpdf</tt> or w2ltex</tt> right behind these lines: $parsed = $parser->parse($to_parse); $mytemp = $helper->path;

And this is my little class file w2lImages.php</tt>:

<?php define('W2L_ImageDir', "Bilder"); define('W2L_ImageTitle', "Abbildung");

class w2lImages {

public static function processImages($parsed, $mytemp) {       $matches = array; $matchCount = preg_match_all('/(.*)<\/IMAGE>/', $parsed, $matches); if ($matchCount > 0) {               $cntr = 0; foreach ($matches[1] as $link) {                       $imgTag = $matches[0][$cntr]; $links = explode("|", $link); $imgFileName = $links[0]; if (strpos($imgFileName, 'jpg') != false) {                               $imgFile = shell_exec("find ./images -name " . $imgFileName); if ($imgFile) {                                       $imgFiles = explode("\n", $imgFile); foreach ($imgFiles as $if) {                                               if (strlen($if) > 0 && strpos($if, "thumb") == false) $imgFile = $if; }                                       if (!file_exists($mytemp. "/" . W2L_ImageDir)) {                                               mkdir($mytemp . "/" . W2L_ImageDir, 0777); }                                       $copied = copy($imgFile, $mytemp . "/" . W2L_ImageDir . "/" . $imgFileName); if ($copied) {                                               $imgCaption = (isset($links[1])) ? $links[1] : W2L_ImageTitle. " " . $cntr; $imgLatex = '\begin{figure}[htb]'. "\n"; $imgLatex .= '\centering'. "\n"; $imgLatex .= '\includegraphics[width=\textwidth]{'. $imgFileName. '}' . "\n"; $imgLatex .= '\caption{'. $imgCaption. '}' . "\n"; $imgLatex .= '\label{fig:'. W2L_ImageTitle. $cntr. '}' . "\n"; $imgLatex .= '\end{figure}'. "\n"; $parsed = str_replace($imgTag, $imgLatex, $parsed); }                                       else {                                               $parsed = str_replace($imgTag, "Image could not be copied: " . $imgFileName, $parsed); }                               }                        }                        else {                               $parsed = str_replace($imgTag, "Image is not a JPG: " . $imgFileName, $parsed); }                       $cntr++; }               return $parsed; }       else {               return $parsed; } }

} ?>

This will result in images included like this: \begin{figure}[htb] \centering \includegraphics[width=\textwidth]{ImageName.jpg} \caption{Abbildung 1} \label{fig:Abbildung1} \end{figure}

Any comments are welcome ;) http://blog.stefan-macke.de

-

Hi Stefan,

I've cut down your code to this snippet that does case-insensitive matching for image: tags + supports image size in centimeters: Inside internalLinkHelper in w2lParser.php:

if(preg_match("/(?i)image:/",$link, $matches)) { $parts = preg_split("/\|/", $link); $imagename = str_replace($matches[0], '', array_shift($parts)); $imgwidth = "10cm"; foreach ($parts as $part) { if (preg_match("/\d+px/", $part)) continue; if (preg_match("/(\d+cm)/", $part, $widthmatch)) { $imgwidth = $widthmatch[1]; continue; }

if (preg_match("/thumb|thumbnail|frame/", $part)) continue; if (preg_match("/left|right|center|none/", $part)) continue; $caption = trim($part); }               $title = Title::makeTitleSafe( NS_IMAGE, $imagename ); $file = new Image( $title ); $file->loadFromFile; $imagepath = $file->getImagePath; $title = $file->getTitle->getText; return "\\begin{center} \\resizebox{".$imgwidth."}{!}{\includegraphics}\\\\ \\textit\end{center}\n";

Given a tag like: it will output LaTeX like: \begin{center} \resizebox{20cm}{!}{\includegraphics{/var/www/mediawiki/images/a/a1/Colombes.jpg}}\\ \textit{Colombes, France}\end{center}

I think there's a lot of good solutions on this page, now we only need Hans-Georg to choose one for the next release.

Cheers, Ole Dahle

namespace problems
If you're not using custom namespaces the extension will stop since it tries to search $wgExtraNamespaces. Is that LaTeX namespace required? If so - for what? --Flominator 10:32, 14 August 2007 (UTC)
 * Solved, see front page. Anyway it should better print what's wrong instead of simply crashing. --Flominator 10:49, 14 August 2007 (UTC)
 * Yeah, the extension should print an error. It's a little mistake, as the extra namespace is only required, if you are using the pdf-export-feature. Will be fixed in v.0.5, which is not too far away. Sorry for the inconvenience. HG 12:16, 14 August 2007 (UTC)

Problem with 0.6.1
Both, the dev-version and the one from Google produce this message: Fatal error: Call to undefined method Image::getPath in C:\XAMPP\htdocs\itswiki\extensions\w2l\w2lParser.php on line 1025 regards, --Flominator 05:52, 14 September 2007 (UTC)
 * Works fine for me. Might be an issue regarding the filename or you're not using Mediawiki 1.11, which is required to run W2L v.0.6 and above due to several changes to local files which were introduced in Mediawikiversions since 1.9. --HG 09:48, 14 September 2007 (UTC)
 * I go this same error with MW1.10, so upgraded to 1.11 as suggested, now new error when trying to export pdf:

Fatal error: Call to a member function getTimestamp on a non-object in /var/lib/mediawiki-1.11.0/extensions/wiki2latex/w2lExporter.php on line 365
 * The relevant section of w2lExporter.php</tt> is

357                // Get Template-Vars... 358    359                 $template_vars = $w2l_vars; 360                $template_vars = $helper->getTemplateVars($to_parse); 361      //If title was not set by a tag, use page title 362                // Put some special variables in the template vars 363    364                 $rev = Revision::newFromTitle($wgTitle); 365                $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp), true ); 366                if(!in_array("Title", $template_vars)) { 367                        $template_vars['Title']  = $title; 368                }    369                 $template_vars['revision timestamp'] = $date; 370                $template_vars['revision user'] = $rev->getUserText; 371                $template_vars['revision id'] = $rev->getId; 372                $template_vars['page id'] = $helper->getArticleId;
 * So $rev</tt> must be null, is it allowed to be, and if so, does this just need an exception test? Any suggestions? I had no problem with the update.php</tt> on the database. Hoogs 15:54, 21 September 2007 (UTC)
 * That's odd. I can't reproduce that error. In case you don't use the template tags, which are defined by these lines, you can put the lines 364-372 into a comment. Might not be the best way, but should work. But I added a Null</tt>-check to the current code. So it should be fixed in w2l 0.6.2. --HG 17:36, 21 September 2007 (UTC)
 * Thanks. Actually there is another problem that may be the cause.  I'd set up address redirection in apache2 for MW1.10, but now for some reason although articles are displayed properly in MW1.11, when I go to the edit tab, the edit page for "Index.php" comes up, for any page.  This probably affects your code too, I'll post when resolved. Hoogs 01:57, 22 September 2007 (UTC)

0.6.1 on Ubuntu server
Running Ubuntu 6.06.1 LTS LAMP server running MW1.11 (upgraded from MW1.10) with the following extensions:


 * Pdf, prints a page as pdf, Thomas Hempel
 * Cite, adds tags for citations, Ævar Arnfjörð Bjarmason
 * ParserFunctions, enhance parser with logical functions, Tim Starling
 * StringFunctions (version 1.9.3)

Editing Index.php
Pages were displaying ok, but upon editing, a generic "Editing Index.php" page would come up. Turns out there is a simple fix, adding:

$wgUsePathInfo = false;

to LocalSettings.php</tt>

Wiki2LaTeX extension installation and testing
The following problems/fixes relate to the pdf export functionality only which was the quickest "so what can this extension do?" test.

Originally, I had put the extension in a directory named extensions/wiki2latex</tt> but the path extensions/w2l</tt> is hardwired into the php (it may be worthwhile emphasising this a bit more), so corrected.

These warnings were displayed: Warning: mkdir [function.mkdir]: Permission denied in /var/lib/mediawiki-1.11.0/extensions/w2l/w2lExporter.php on line 691

Warning: chmod [function.chmod]: No such file or directory in /var/lib/mediawiki-1.11.0/extensions/w2l/w2lExporter.php on line 692

Warning: file_put_contents(extensions/w2l/tmp/tmp-1190454669-678688419/Main.tex) [function.file-put-contents]: failed to open stream: No such file or directory in /var/lib/mediawiki-1.11.0/extensions/w2l/w2lExporter.php on line 402

Warning: chdir [function.chdir]: No such file or directory (errno 2) in /var/lib/mediawiki-1.11.0/extensions/w2l/w2lExporter.php on line 413

The directory extensions/w2l</tt> must be writeable so this was fixed (lazily) with


 * 1) chmod -R a+w /extensions/w2l

Maybe worth mentioning permissions on the extension directory tree. This error came up in the latex log:

LaTeX Error: File `utf8x.def' not found.

This was fixed in ubuntu with


 * 1) aptitude install latex-ucs

It may be worth starting a list of dependencies. For some wiki pages, even though there was no fatal error in the latex log and Main.{tex,pdf}</tt> etc. seemed ok, the extension would display the message

w2lParser::getContentByTitle: Artikel existiert nicht: Vorlage:Date

Google translated:

w2lParser:: getContentByTitle: Article does not exist: Collecting main: DATE

I couldn't resolve this. The generated pdf seemed to pick up  $$...$$ </tt> perfectly. However 0.6.1 did not display these correctly:

Basic stuff I really need
 * <tt> ... </tt> (have used these to centre equations)
 * tables

Nice-to-haves
 * (currently nil, I haven't tested sufficiently yet)

I have never really found a decent converter and this looks promising so congratulations and many thanks!! I now use my mediawiki as a primary documentation system at work, so if we can build the bridge wiki -> latex/pdf this will be an amazing tool. Hoogs 11:25, 22 September 2007 (UTC)
 * The error saying <tt>"Artikel existiert nicht: Vorlage:Date"</tt> means, that a template could not be found. Will be fixed soon. Regarding the issue with <tt> $$$$ </tt> I'd be interested what exactly does not display right, as that should be fine for long time now. <tt> ... </tt> support is on the todo-list. --HG 17:57, 23 September 2007 (UTC)
 * Sorry, misunderstanding, math tags work fine, I have changed grammar above to make that clearer. What about tables? I think you would need table conversion to be able to say you have complete basic-level functionality. Hoogs 23:32, 23 September 2007 (UTC)
 * Tables are supported. At least simple ones without 'rowspan'-ing or 'colspan'-ing. You just need to add a small attribute, which is documented at Extension talk:Wiki2LaTeX/Development/w2lParser.php. I hope it works for you.--HG 07:03, 24 September 2007 (UTC)