Extension talk:Wiki2LaTeX

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)