In the TexStudio user mannual I have found the answer of the question. I am sharing it with the hope that it will help others.
- Add a New Macro
- Give some name to the macro
- In
Trigger
write (?highlighted-as:numbers,math-delimiter,math-keyword)_
Type
is Normal
Latex Content
is _{%|}
Although replacing ^
with ^{%|}
is little bit more tricky, because ^
has special meanings in a regular expression. For that change the third bullet above by
- In
Trigger
write (?highlighted-as:numbers,math-delimiter,math-keyword)\^
And change fifth bullet above by
The only drawback of this method is that the macro will also activated by pressing the trigger if the cursor is touching any math code even if it is outside the math code. For example: A$ some math code $B
here you can trigger the macro even if the cursor is in between A
and $
or in between B
and $
because $
is a math code and the cursor is touching $
. However, if the cursor is not touching any math code the macro will not be executed by trigger.
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);
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:
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.)
Best Answer
You can use
Edit -> Go to -> Go Back / Go Forward
(Alt+Left/Right).In the standard configuration this is also available via the yellow arrows in the central vertical toolbar. Also, if your mouse or keyboard have special keys for this operation, they should work.