jlelong/LaTeX-Workshop-wiki

Use sumatraPdf &inverse-search SyncTex in windows (11, actually) with vscode 1.62.3

Closed this issue · 18 comments

the SyncTex can be used with the following settings:

In sumatraPDF: (THIS IS IMPORTANT)

InverseSearchCmdLine = "C:\Users\<Username>\AppData\Local\Programs\Microsoft VS Code\Code.exe"  -g "%f":"%l"// the part of cli.js is deleted
EnableTeXEnhancements = true

In vscode: (other settings are the same as normal)

"latex-workshop.view.pdf.external.synctex.args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "-reuse-instance",
        "-inverse-search",
        "code \"C:\\Users\\<Username>\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"", 
        //In this place, whether the cli.js is deleted seems like make no sense
        "%PDF%",
    ],

P.S. it seems like that the cli.js is not necessary, but i do not know the reason why the deletion will make sense since i have no idea about the file or anything about the principle of SyncTex, but it truly works for me. By the way, this way seems like also solve the problem that the SyncTex fails when use inverse search and open the cli.js in vscode instead.

See the commit 2544980

Change to the default install directory on Windows.
See https://code.visualstudio.com/docs/setup/windows

The path, "D:\Code\VS code\Microsoft VS Code\Code.exe", might be the portable version's one.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

See the commit 2544980

Change to the default install directory on Windows.
See https://code.visualstudio.com/docs/setup/windows
The path, "D:\Code\VS code\Microsoft VS Code\Code.exe", might be the portable version's one.

thanks, i already changed it.

Dear All,
I’ve found the solution.
It looks that “code.exe” doesn’t support cli arguments.
The solution is using “code.cmd” instead of “code.exe”,
In detail, I changed the inverse search command part in “settings.json” like as follows:
“code.cmd -r -g “%f:%l””,
Hope this helps you.
Thank you.

Dear All, I’ve found the solution. It looks that “code.exe” doesn’t support cli arguments. The solution is using “code.cmd” instead of “code.exe”, In detail, I changed the inverse search command part in “settings.json” like as follows: “code.cmd -r -g “%f:%l””, Hope this helps you. Thank you.

Can you please paste your code of synctex in settings.txt of SumatraPDF and settings.json in VSCode?
Thank you very much.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

I kind of know the reason of the problem now.

I've found out that when you use syntex to open PDF file from VSCode when it's not initially opened in SumatraPDF, VSCode will change the value of InverseSearchCmdLine in settings.txt of SumatraPDF to code "C:\Users\zhang\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l".

There's a way to prevent the change to take place:

  • use ctrl + alt + v to open the PDF file at first
  • then you can use ctrl + alt + j to forward-search, VSCode will not change the InverseSearchCmdLine value in this way

sry, after a few try, I found out that SumatraPDF has to be opened before the ctrl + alt + v action

Dear All, I’ve found the solution. It looks that “code.exe” doesn’t support cli arguments. The solution is using “code.cmd” instead of “code.exe”, In detail, I changed the inverse search command part in “settings.json” like as follows: “code.cmd -r -g “%f:%l””, Hope this helps you. Thank you.

Can you please paste your code of synctex in settings.txt of SumatraPDF and settings.json in VSCode? Thank you very much.

I haven't changed anything in settings.txt of SumatraPDF.
It seems just modifying settings.json works.
It is as follows:
"latex-workshop.view.pdf.viewer":"external",
"latex-workshop.view.pdf.external.viewer.command": "path\to\SumatraPDF.exe",
"latex-workshop.view.pdf.external.viewer.args": [
"-forward-search",
"%TEX%",
"%LINE%",
"-reuse-instance",
"-inverse-search",
// "code "path\to\cli.js" -r -g "%f:%l"", //this line should be modified.
"code.cmd -r -g "%f:%l"", //Note that I used code.cmd, not "code" nor "code.exe" and cli.js is not needed
"%PDF%"
],

"latex-workshop.view.pdf.external.synctex.command": "SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
"-forward-search",
"%TEX%",
"%LINE%",
"-reuse-instance",
"-inverse-search",
// "code "path\to\cli.js" -r -g "%f:%l"",
"code.cmd -r -g "%f:%l"",
"%PDF%"
],

I hope you will find that althought the command prompt bothers you, this solution works.
If you have any problems, please comment.
Thank you.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

I kind of know the reason of the problem now.

I've found out that when you use syntex to open PDF file from VSCode when it's not initially opened in SumatraPDF, VSCode will change the value of InverseSearchCmdLine in settings.txt of SumatraPDF to code "C:\Users\zhang\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l".

There's a way to prevent the change to take place:

  • use ctrl + alt + v to open the PDF file at first
  • then you can use ctrl + alt + j to forward-search, VSCode will not change the InverseSearchCmdLine value in this way

sry, after a few try, I found out that SumatraPDF has to be opened before the ctrl + alt + v action

It is because -inverse-search phrase in the settings.json. In the document for SumatraPDF(https://www.sumatrapdfreader.org/docs/Command-line-arguments), you can find the explanation like below:
-inverse-search <command-line> : sets the command line to be used for performing an inverse search from a PDF document (usually back to a LaTeX source file). The inverse search command line can also be set from the Setting dialog. Use the variable %f for the current filename and %l for the current line. [Deprecated]: This setting is exposed in the Options dialog after the first PDF document with corresponding .synctex or .pdfsync file has been loaded. Alternatively, use the corresponding advanced setting instead.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

I kind of know the reason of the problem now.
I've found out that when you use syntex to open PDF file from VSCode when it's not initially opened in SumatraPDF, VSCode will change the value of InverseSearchCmdLine in settings.txt of SumatraPDF to code "C:\Users\zhang\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l".
There's a way to prevent the change to take place:

  • use ctrl + alt + v to open the PDF file at first
  • then you can use ctrl + alt + j to forward-search, VSCode will not change the InverseSearchCmdLine value in this way

sry, after a few try, I found out that SumatraPDF has to be opened before the ctrl + alt + v action

It is because -inverse-search phrase in the settings.json. In the document for SumatraPDF(https://www.sumatrapdfreader.org/docs/Command-line-arguments), you can find the explanation like below: -inverse-search <command-line> : sets the command line to be used for performing an inverse search from a PDF document (usually back to a LaTeX source file). The inverse search command line can also be set from the Setting dialog. Use the variable %f for the current filename and %l for the current line. [Deprecated]: This setting is exposed in the Options dialog after the first PDF document with corresponding .synctex or .pdfsync file has been loaded. Alternatively, use the corresponding advanced setting instead.

I can now understand the reason for the specific file/ application opening order.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

I kind of know the reason of the problem now.
I've found out that when you use syntex to open PDF file from VSCode when it's not initially opened in SumatraPDF, VSCode will change the value of InverseSearchCmdLine in settings.txt of SumatraPDF to code "C:\Users\zhang\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l".
There's a way to prevent the change to take place:

  • use ctrl + alt + v to open the PDF file at first
  • then you can use ctrl + alt + j to forward-search, VSCode will not change the InverseSearchCmdLine value in this way

sry, after a few try, I found out that SumatraPDF has to be opened before the ctrl + alt + v action

It is because -inverse-search phrase in the settings.json. In the document for SumatraPDF(https://www.sumatrapdfreader.org/docs/Command-line-arguments), you can find the explanation like below: -inverse-search <command-line> : sets the command line to be used for performing an inverse search from a PDF document (usually back to a LaTeX source file). The inverse search command line can also be set from the Setting dialog. Use the variable %f for the current filename and %l for the current line. [Deprecated]: This setting is exposed in the Options dialog after the first PDF document with corresponding .synctex or .pdfsync file has been loaded. Alternatively, use the corresponding advanced setting instead.

I've tried your method, and it generally works.
Thank you very much for that. You have saved me.
But I've noticed that a window pops out before the line shows in VSCode. That's completely out of my knowlege. (I'm just generally lack of any programming common sense)
I wonder if the same phenomenon occurs in your case as well, and if possible, the reason for it.

@jlelong hi, there is a problem that this solution (often) fail when someone open the pdf file from vscode, but (always) works for opening the external pdf viewer separately.

I kind of know the reason of the problem now.
I've found out that when you use syntex to open PDF file from VSCode when it's not initially opened in SumatraPDF, VSCode will change the value of InverseSearchCmdLine in settings.txt of SumatraPDF to code "C:\Users\zhang\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l".
There's a way to prevent the change to take place:

  • use ctrl + alt + v to open the PDF file at first
  • then you can use ctrl + alt + j to forward-search, VSCode will not change the InverseSearchCmdLine value in this way

sry, after a few try, I found out that SumatraPDF has to be opened before the ctrl + alt + v action

It is because -inverse-search phrase in the settings.json. In the document for SumatraPDF(https://www.sumatrapdfreader.org/docs/Command-line-arguments), you can find the explanation like below: -inverse-search <command-line> : sets the command line to be used for performing an inverse search from a PDF document (usually back to a LaTeX source file). The inverse search command line can also be set from the Setting dialog. Use the variable %f for the current filename and %l for the current line. [Deprecated]: This setting is exposed in the Options dialog after the first PDF document with corresponding .synctex or .pdfsync file has been loaded. Alternatively, use the corresponding advanced setting instead.

I've tried your method, and it generally works. Thank you very much for that. You have saved me. But I've noticed that a window pops out before the line shows in VSCode. That's completely out of my knowlege. (I'm just generally lack of any programming common sense) I wonder if the same phenomenon occurs in your case as well, and if possible, the reason for it.

Good to hear.
And, for your question, yes, like you mentioned, a command prompt pops up like this when you double-click somewhere on the pdf file:
cmd
I guess this is because *.cmd file runs on command prompt environment.
I have no idea how to hide the prompt window yet.
I am going to do some resesarch.

@zycsss You should check this:https://forum.sumatrapdfreader.org/t/inverse-search-not-performed-for-vs-code-exe/4486/20
Although I haven't tried. Maybe when I wake up in the morning, I am going to test it too.

@zycsss You should check this:https://forum.sumatrapdfreader.org/t/inverse-search-not-performed-for-vs-code-exe/4486/20 Although I haven't tried. Maybe when I wake up in the morning, I am going to test it too.

I've tried the method and it works fine.
Here's my example:

"latex-workshop.view.pdf.external.synctex.args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "\"C:\\Users\\<Username>\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" \"C:\\Users\\<Username>\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" --ms-enable-electron-run-as-node -r -g \"%f:%l\"",
    "%PDF%"
  ],

@zycsss Yeah, I also have found it works.
Actually, I used the args for the external viewer args, but it seems unnecessary.
Thank for notifying me.

@zycsss @syehoonkim It seems that you have figured out a reliable configuration. Could one of you make a PR?

@jlelong Let me do this. But, can you please tell me how to in detail? Should I make a pull request on FAQ doc?

@jlelong @syehoonkim I've already created one. I've modified the ducumentation.

@zycsss Thanks!