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)