2

I would like to achieve the following functionality in TeXstudio 2.12.4 (hg 6476:3e5c3afa4719).

During a quick build (Build and view, F5 shortcut), and only when I invoke this command, I would like TeXstudio to automatically insert some (pre-set) lines in my TeX code.

I'll give an example. Suppose this is what my document looks like:

% Code 1: What I want to see as I type in my editor
\documentclass[]{article}

\begin{document}
    Hello world.
\end{document}

When I press compile (F5) however, I would like TeXstudio to automatically add in a line after the \documentclass line, like so:

% Code 2: What I want the effect to be (compilation result)
\documentclass[]{article}
\usepackage{xcolor}\color{red} % This is automatically added in during compilation.

\begin{document}
    Hello world.
\end{document}

and the effect I should get is an all-red text 'Hello world' document.

Catch: The catch here is that I wish for the line \usepackage{xcolor}\color{red} to never show up in my editor, so this line should be (temporarily, and automatically) added in the backend by TeXstudio, and deleted right after compilation is done.

To sum up, I hope to see Code 1 in my TeXstudio editor (both before and after compilation), but have the result of Code 2 after I press compile/ quick build etc.

Possible solutions

I understand that TeXstudio supports the use of script (see here), but I'm not sure what the proper syntax is for inserting a custom string into my TeX code, for example.

Another approach to take might be the one from the solution in this question previously asked here on TeX.SE, only I wish to pass a command before TeXstudio compiles to automatically insert a line into my TeX code, and to pass a command after compilation to delete that particular line from my code.

Is this feasible/achievable?

1 Answer 1

1

You cannot bind a script to "before-command-run" since there is currently no such trigger available.

However, you can write your own script, which does the work and calls "Build & View" at some point. You can also assign the "F5" shortcut to it in the options if you like. So this does essentially what you want (even though I don't understand the purpose).

%SCRIPT

function onDocumentClassFound(cur) {
    cur.movePosition(1, cursorEnums.NextLine);
    cur.insertText("\\usepackage{xcolor}\\color{red}\n");   
    app.getManagedAction("main/tools/quickbuild").trigger();
    app.editUndo();
}

options = "";
scope = editor.document().cursor(0, 0, -1);
editor.search("\\documentclass", options, scope, onDocumentClassFound);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .