# Create texstudio macro to open file from tex tree

texstudio has the nice ability to open included files from within another file by right click on the file name

\include{test}


and then open test from the context menu

This works for files which are either in the current working folder or for which a relative or absolute path is specified. It does not work for files which are within the search path of latex.

# Question:

Is it possible to create a user macro (or similar) to open a file from the latex search path?

From the command line, I'd use

open kpsewhich ushyphex.tex


to open them. I tried to write a texstudio user macro

%SCRIPT
system(open kpsewhich cursor.selectedText());



(and for simplicity had .tex included in the selected text) however this leads to an error

• Does it work if you enclose the open ... in quotes?
– Troy
Feb 28, 2018 at 13:10
• @Troy Thanks for your suggestion. Please see my edit - it seems that the  now are not properly recognised and open tries to open two separate files... Feb 28, 2018 at 13:15
• It doesn't answer the question, but I take the ability of TS to do so is already build in somehow. When an error is indicated in compilation in a line within a package with a click you get the sty or whatever the case is to open. I have no idea how to exploit this for the purpose, but perhaps someone else has... Feb 28, 2018 at 13:26
• @gusbrs I did not check the source code, but I imagine this could also be done via clever parsing of the log file which contains the absolute path. Feb 28, 2018 at 13:36
• @gusbrs However the texstudio source code seems to include some interesting fuctions, e.g. KpathSeaParser .... Feb 28, 2018 at 13:41

I'll be addressing the main question: Is it possible to create a user macro (or similar) to open a file from the latex search path?

Really, the whole issue is that TeXstudio does not (by default) involve the shell when executing its commands, which includes the system(...) command used in your user macro. This is outlined in the TXS user manual, under Section 1.3.2, Shell functionality. So in order for your commands (denoted by <cmds> below) to run as they would in Terminal / Command Prompt, you would have to invoke shell explicitly in your user command:

For Mac OS / Linux:

sh -c "<cmds>"


For Windows:

cmd /C "<cmds>"


as per the User manual.

With that in mind, the rest is fairly simple: the result from cursor.selectedText() can just be concatenated as you would a string in the argument for system(...).

In your case (Mac OS), something to this effect will suffice:

%SCRIPT
system('sh -c "open kpsewhich ' + cursor.selectedText() + '"')
`

Do note the placements of the quotes (single / double quotes) in the above command.