Re: Latex e Perl

#81449
Up
0
Down
::

EmilLask” post=80709

Eppure ci sarà un modo per la gestione di tabelle lunghe e complesse che non possono essere riordinate manualmente senza sprecare una quantità enorme di tempo.

Fermo restando che la via maestra, come ti hanno già consigliato, sarebbe scrivere la tabella in un foglio di calcolo, riordinarla lì e importarla in LaTeX solo alla fine, ecco uno script che fa (dovrebbe fare :)) quello che desideri:

`// TeXworksScript
// Title: O&rdina per colonna
// Description: Ordina il codice di una tabella per colonna
// Author: Nessuno
// Version: 0.1
// Date: 2013-01-02
// Script-Type: standalone
// Context: TeXDocument
// Shortcut: Ctrl+K, Ctrl+R

// Per supportare versioni di Qt precedenti alla 4.7
if(typeof(String.prototype.trim) == “undefined”)
{
String.prototype.trim = (function() {
var re = /^[\s\n]+|[\s\n]+$/g;
return function() { return this.replace(re, “”); };
})();
}

function parseLaTeXTable(text)
{
// Facciamo una cosa molto semplice ma abbastanza efficace
// altrimenti il codice qui si complicherebbe di molto
var regex = /^(?:(?!\\\\).|\n)+(\\\\\n*|(?=$))/;
var rows = [];
for (var i = 0; (matches = regex.exec(text)) != null; i++) {
var s = matches[0], row = [ s ];
for (var pos = s.indexOf(“&”); pos >= 0; pos = s.indexOf(“&”, pos)) {
if (pos == 0 || s[pos-1] != '\\') {
row[row.length – 1] = s.substr(0, pos);
row[row.length] = s = s.substr(pos + 1);
pos = 0;
}
else pos++;
}
row[row.length – 1] = s.substr(0, s.length-matches[1].length);
rows = row;

text = text.substring(matches[0].length);
}
return rows;
}

var selection = TW.target.selection;
if (selection != “”)
{
var table = parseLaTeXTable(selection);
var column = TW.getInt(null, “Ordinamento tabella”, “Inserisci il numero di colonna (a partire da 0) sulla quale ordinare:\n”, 0, 0);
if (typeof column == “number”) {
var code = table.sort(function (a,b) {
return column < a.length && column < b.length ? a[column].trim().toLowerCase().localeCompare(b[column].trim().toLowerCase()) : a.length - b.length; }).map(function(row) { return row.join("&"); }).join("\\\\\n"); var m = /\\\\(\n*)$/.exec(selection); if (m) { code += m[0]; } TW.target.insertText(code); } }`

Go to top