4

TexStudio has a nice option of aligning given tabular environment by choosing LaTeX > Manipulate Tables > Align Columns from the menu or clicking this icon on the toolbar:

enter image description here

So something rough like this

\documentclass[10pt,a4paper]{article}
\begin{document}
    \begin{tabular}{|c|c|c|}
        \hline 
        fo1 & fo2 & fo3 \\ 
        \hline 
        foob1 & foob2 & foob3 \\ 
        \hline 
        foobar1 & foobar2 & foobar3 \\ 
        \hline 
    \end{tabular} 
\end{document}

becomes more readable:

\documentclass[10pt,a4paper]{article}
\begin{document}
    \begin{tabular}{|c|c|c|}
        \hline
          fo1   &   fo2   &   fo3   \\ \hline
         foob1  &  foob2  &  foob3  \\ \hline
        foobar1 & foobar2 & foobar3 \\ \hline
    \end{tabular} 
\end{document}

But I feel really stupid as I cannot find the way to beautify the code for all the tables in a document at once. By choosing this option even if the entire document is selected I can only automatically align one table at a time, so I need to manually click through all the tables.

Is there something I am missing?


Similar question also goes for the alignment about =sign, e.g. in BIB-files, where

@article{Foo1970,
    title = {Lorem Ipsum},
    pages = {451},
    number = {42},
    date = {1970-01-01},
    journaltitle = {TUG},
    author = {Foo, Bar},
}

should look like

@article{Foo1970,
    title        = {Lorem Ipsum},
    pages        = {451},
    number       = {42},
    date         = {1970-01-01},
    journaltitle = {TUG},
    author       = {Foo, Bar},
}

or in preamble when certain parameters are passed to the package:

\usepackage[
  math-style = ISO,
  bold-style = ISO,
  partial = upright,
  nabla = upright
]{unicode-math}

visually better:

\usepackage[
  math-style = ISO,
  bold-style = ISO,
  partial    = upright,
  nabla      = upright
]{unicode-math}

Is there a unified way to align the code for entire document using TexStudio (or other means as a last resort) around & and =?

  • 1
    Not TeXstudio, but there is a Perl-script called latexindent included in TeX Live/MikTeX that might be of interest, see e.g. tex.stackexchange.com/questions/248166/… – Torbjørn T. Sep 4 '17 at 10:36
  • @TorbjørnT. Thanks for suggesting latexindent. Actually latexindent -w file.tex does the job with aligning tables (though differently as TexStudio -- the latter also takes cells alignment into account), but I cannot figure out how to use it for sorting out entries with=. I flipped the manual and it seems like I need to craft my own settings.yaml file and somehow define the rules there (?) Also I use latexmk for compilation (too much Perl, I know), but it seems like latexindent is more suited for arara. If someone could elaborate a bit more on this script, this would be helpful. – andselisk Sep 4 '17 at 10:57
  • @andselisk LaTeX > Manipulate Tables > Align Columns in TeXstudio is a dedicated and specialized tool to align a tables. = and application to a whole document is beyond its scope. For the latter, you could write a marco that searches for all tables and applies the alignment to each. – Tim Hoffmann Sep 5 '17 at 21:16
3

Since you already have solutions for the Table alignment with &, here's a solution for the alignment with =. It's a user script that is written more for clarity than efficiency (hopefully!), and keep in mind that I'm not at all fluent in QTScript.

User Script for Aligning = in TeXstudio

Create a new script:

%SCRIPT
if (cursor.hasSelection()){
    var tl = cursor.selectedText().split("\n");
} else {
    var tl = editor.document().textLines();
}
var PreString = Array(tl.length-1),
var PostString = Array(tl.length-1);
var tempstr, eq_index, max_eq_index=0;
var LineNo = cursor.lineNumber(), ColNo = cursor.columnNumber();

// Extract strings before and after "="
// And remove trailing spaces
for (var i=0;i<tl.length;i++){
    tempstr = tl[i];
    eq_index = tempstr.indexOf('=');
    if (eq_index <0){
        PreString[i] = tempstr.replace(/\s+$/, '');
        PostString[i] = '';
    } else {
        PreString[i] = tempstr.slice(0,eq_index).trim();
        PreString[i] = "\t" + PreString[i];
        PostString[i] = tempstr.slice(eq_index+1).trim();
        max_eq_index = Math.max(PreString[i].length, max_eq_index);
    }
}

// Add spaces to PreStrings so all the "=" align
// Concat PreString and PostString
var t = "";
for (var i=0;i<tl.length;i++){
    if (PostString[i].length>0){
        PreString[i] += Array(max_eq_index-PreString[i].length+1).join(" ");
        t += PreString[i] + ' = ' + PostString[i] + "\n";
    } else {
        t += PreString[i] + "\n";
    }
}

// Print text and tie up loose ends
t = t.slice(0, -1);
if (cursor.hasSelection()){
    cursor.replaceSelectedText(t);
    cursor.clearSelection();
} else {
    editor.setText(t)
}
cursor.moveTo(LineNo, ColNo);

mac

Test bib files

Here are some bib entries to try out on, as I have in my .gif.

% Some bib examples

@article{bib1,
    title = {Lorem Ipsum},
    pages = {451},
    number = {42},
    date = {1970-01-01},
    journaltitle = {TUG},
    author = {Foo, Bar},
}

@misc{website:fermentas-lambda,
    author = "Fermentas Inc.",
    title = "Phage Lambda: description \& restriction map",
    month = "November",
    year = "2008",
    url = "http://www.fermentas.com/techinfo/nucleicacids/maplambda.htm"
}

@article{bib3,
    title={Lorem Ipsum},
    pages={451},
    number={42},
    date={1970-01-01},
    journaltitle={TUG},
    author={Foo, Bar},
}

@article{bib4,
title = {Lorem Ipsum},
pages = {451},
number = {42},
date = {1970-01-01},
journaltitle = {TUG},
author = {Foo, Bar},
}

Result:

mwe

Notes:

  • I have yet to figure out how to localize the effect of this user script to a user's selected text, so maybe that will come later if I get around to doing it. Now the script takes into account selected text, and the alignment effect can thus be localized (see gif).

  • Some bugs squished -- the script should be more consistent now. (If there is anyone using this, please try it out and let me know if there are further bugs with this script, thanks.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.