Latex-workshop: Synctex inverse search doesn't work half the time (and How I got forward search to work for SumatraPDF)

Created on 18 May 2018  ·  42Comments  ·  Source: James-Yu/LaTeX-Workshop


Description

In SumatraPDF, double clicking should initiate a inverse search, but it doesn't work, IF the SumatraPDF instance is fired up by VS Code. If the SumatraPDF is opened independently, backward search will work.


LaTeX Workshop Output


Nothing here.

Developer Tools Console


Nothing here.

How to reproduce

  1. Get any .tex compiled to pdf with synctex enabled.
  2. Put this in user settings. Use "external" as pdf viewer.

    "latex-workshop.view.pdf.external.synctex": {
    "command": "SumatraPDF",
    "args": [
      "-forward-search",
      "%TEX%",
      "%LINE%",
      "-reuse-instance",
      "-inverse-search",
      "\"code.exe\" -g \"%f:%l\"",
      "%PDF%",
    ]
    },
    
  3. In VS Code, use 'Synctex from cursor', the forward search works. Inverse search doesn't. In the fired up SumatraPDF, with double clicking, nothing happens.

    If you check the fired up SumatraPDF, in settings - Options - Set inverse search command-line you will see "code.exe" -g "%f:%l".

  4. However, if you fire up SumatraPDF independently, and put this into settings - Options - Set inverse search command-line

    "code.exe" -g "%f:%l"
    

    The inverse search actually works. This just let SumatraPDF to execute the commandline with 'goto` command to code.

  5. I don't understand why the SumatraPDF instance initiated by VS Code can't perform inverse search, but the independent ones can. I also notice that, the SumatraPDF instance fired up by VS Code will also exit, if you close VS Code. Are they connected somehow? How to get around this problem?

Thank you.

Expected behavior

Described above.

Actual behavior

Described above.

Additional Information

I have read previous external pdf viewer issues, and I do understand the frustration of dealing with them. Here I would like to contribute a little bit by getting SumatraPDF to work.

I am using win7, VS Code 1.23.1 zip. Updated LW.

Synctex forward search with SumatraPDF

The default setting in LW doesn't work for SumatraPDF. To enable SumatraPDF with forward synctex search, put this in the user-settings:

"latex-workshop.view.pdf.external.synctex": {
"command": "SumatraPDF",
"args": [
  "-forward-search",
  "%TEX%",
  "%LINE%",
  "-reuse-instance",
  "-inverse-search",
  "\"code.exe\" -g \"%f:%l\"",
  "%PDF%",
]
},

This is assuming SumatraPDF and VS Code are in the system PATH.

need help

Most helpful comment

@qcts33 Black window occurs if you use C:\Program Files\Microsoft VS Code\bin\code.cmd for invoking reverse sync. It can be very slow and is annoying as the black window blocks the screen.

The code.cmd just makes a call to code.exe which can be directly used in SumatraPDF to circumvent the black box. Based on where your vscode is installed, your inverse search setting in SumatraPDF should look something like
"C:\Program Files\Microsoft VS Code\Code.exe" "C:\Program Files\Microsoft VS Code\resources\app\out\cli.js" -g "%f":"%l"

All 42 comments

The external program was executed using child_process, which, as its name implies, opens the viewer as a child process in vscode. This can explain all your observations.

However, there is no work-around on the extension side. You may try using CMD.exe someviewer.exe instead of its current form.

All in all, external viewer support is unfortunately a second-class citizen in the context, and there is no obvious and universal solution to your problem.

Dear James,

Thanks for the reply. I respect your work and I like the extension, from the atom times till now. But, the internal pdf viewer does not work in the end. This is simply because VS Code doesn't allow the pdf viewer tab to be placed in a separate window (ctrl+k, o doesn't work). In general, the font size of the pdf will NOT be suitable for reading in a tab size. There is no priori that the font size of your writing tab and the font size of the rendered pdf will happen to be the same for viewing. For example, if you write scientific paper, usually it's two column, and the internal viewer is just too small.

For this reason, I, and several of my colleges end up switching back to atom or st. Hope you can take this issue into consideration, and maybe come up with something ingenious. Thanks.

Did you try opening pdf in browser?

My understanding is inverse search wouldn't work in browser?
Also, imho "out-source the viewing task to dedicated viewers" seems to be phylosophically a good practice.

It works. Just ctrl+click.

Yes browser works indeed. My apologies.
However, still this is a maybe good work-around, and not a solution. People perfer dedicated viewer to browser for a range of different reasons. Right now it really hurts that, with SumatraPDF for example, one simply can't have inverse-search.

The philosophies might be different.

This extension is designed to be an opinionated LaTeX extension which aims at a "ready-to-eat" experience without external dependencies. Think of the many users who somehow does not want to have another program on their computer.

In Atom, I can use electron API to create a new standalone window, which looks more elegant. This is unavailable in vscode.

I understand this is a messy task. I understand your philosophy as well. I am not complaining or anything, just really my honest opinion, that not having the option will become a problem in the long run. "ready-to-eat", yes, it works right after installation. But does it still work great a few mins into actually using it for productive work? That's when I ran into this issue.

ALso, "cmd.exe SumatraPDF.exe" doesn't seem to work for me.

Is there any way to avoid initiating the external viewer as child_process? I think it's pretty much the last thing needed to get SumatraPDF working flawlessly.

use cmd and list the viewer as an arg. You need to play a bit with the config.

TODO: detached may be the right way.

I will push a release with this fix soon. Please have a check on if the issue is resolved.

Thanks for the fix. I am on vacation for a week but will check as soon as I
am back.

On Tue, May 22, 2018 at 2:30 AM James Yu notifications@github.com wrote:

I will push a release with this fix soon. Please have a check on if the
issue is resolved.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/James-Yu/LaTeX-Workshop/issues/637#issuecomment-390907290,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AYnUpaRNXaz9khtJPtnFj83lPSFoZu2cks5t08y6gaJpZM4UD3yz
.

>

Best Regards,

Alex

Strange, I updated to 5.5.0, but it still doesn't seem to work for me.
And Sumatra still closes after I close VSCode.

@James-Yu Am I correct to assume that inverse search without setting up a different PDF viewer like Sumatra will never be part of the roadmap here? It would be very nice but I don't want to open a new issue if that's infeasible. P.S. Thanks for this awesome extension!

@benlindsay It always works for the built-in viewer.

@James-Yu Thanks for the response! Knowing that I updated npm and ran npm install in the extension folder (since that was an issue for a different extension) and now it works! And that's an amazing feature. Thanks again for making this!

I can not start SumatraPDF by using cmd and list the viewer as an arg.
However, I found another workaround for this problem that I can call C:\Program Files\Microsoft VS Code\bin\code.cmd instead of C:\Program Files\Microsoft VS Code\Code.exe from SumatraPDF. Everything works now, except a black window flash around when doing the inverse search.

I can not start SumatraPDF by using cmd and list the viewer as an arg.
However, I found another workaround for this problem that I can call C:\Program Files\Microsoft VS Code\bin\code.cmd instead of C:\Program Files\Microsoft VS Code\Code.exe from SumatraPDF. Everything works now, except a black window flash around when doing the inverse search.

For default location, "C:\Program Files\Microsoft VS Code\Code.exe" -g "%f:%l" works perfect.

@yzlnew Still not working for me.
Can you double check? For my test, the default location only works if SumatraPDF is launched outside of vscode. When SumatraPDF is started by vscode, the default location does not work.

@qcts33 Pointing to the location doesn't work when SumatraPDF is started by VSCode.

It works. Just ctrl+click.

it would be better to include this into the features(Taster) part. Took time to figure it out by myself.

@qcts33 Black window occurs if you use C:\Program Files\Microsoft VS Code\bin\code.cmd for invoking reverse sync. It can be very slow and is annoying as the black window blocks the screen.

The code.cmd just makes a call to code.exe which can be directly used in SumatraPDF to circumvent the black box. Based on where your vscode is installed, your inverse search setting in SumatraPDF should look something like
"C:\Program Files\Microsoft VS Code\Code.exe" "C:\Program Files\Microsoft VS Code\resources\app\out\cli.js" -g "%f":"%l"

@kman0 That works!!! I should have thought of this earlier. Thank you!!!

@qcts33 Using VSCode installed by Scoop actually solved the previous problem.

@qcts33
soft VS Code\Code.exe" "C:\Program Files\Microsoft VS Code\resourcesapp\outcli.js" -g "%f":"%l"`

This works for me too!

However, every time it'll open a new window for VSCode. Do you have any clues to solve this problem?

@qcts33
soft VS Code\Code.exe" "C:\Program Files\Microsoft VS Code\resourcesapp\outcli.js" -g "%f":"%l"`

This works for me too!

However, every time it'll open a new window for VSCode. Do you have any clues to solve this problem?

Well, I've fixed this by myself for the following settings:

"latex-workshop.view.pdf.external.synctex": {
  "command": "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe",
  "args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "\"C:\\Users\\huangchencheng\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" \"C:\\Users\\huangchencheng\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"",
    "%PDF%"
  ]
},

-r flag here means "reusing last active window". See here.

I have tried your configuration but it doesn't work in my case. I have VSCode in portable mode. Could you help me?

    "latex-workshop.view.pdf.external.synctex": {
        "command": "D:/PortableApps/SumatraPDF/SumatraPDF.exe",
        "args": [
            "-forward-search",
            "%TEX%",
            "%LINE%",
            "-reuse-instance",
            "-inverse-search",
            "\"D:\\dev\\IDEs\\VSCode\\Code.exe\" \"D:\\dev\\IDEs\\VSCode\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"",
            "%PDF%",
        ]
    },

I have tried this line directly from the command line and when clicking in SumatraPDF it is opening a new VSCode window and two files (cli.js and the tex file). I don't know how I can debug this stuff.

Any help is more than welcome.

@jlanza If everything is right, the cli.js file should not open.

  1. Is(check your) forward search working?
  2. Could you post the inverse sync settings in SumatraPDF?
  3. Based on your vscode location, SumatraPDF settings should be
    "d:\dev\IDEs\VSCode\Code.exe" "d:\dev\IDEs\VSCode\resources\app\out\cli.js" -r -g "%f":"%l"
    Does the above open the file and goto line?
  4. Important thing: do not open SumatraPDF externally. Close all SumatraPDF and let vscode open it for you.

@jlanza If everything is right, the cli.js file should not open.
1. Is(check your) forward search working?
It is working without any problem.

2. Could you post the inverse sync settings in SumatraPDF?

Currently is commented.

InverseSearchCmdLine = D:\dev\IDEs\VSCode\Code.exe -r -g "%f:%l"

I tested the configuration above (without comment ;)) and didn't work.

3. Based on your vscode location, SumatraPDF settings should be
   `"d:\dev\IDEs\VSCode\Code.exe" "d:\dev\IDEs\VSCode\resources\app\out\cli.js" -r -g "%f":"%l"`
   Does the above open the file and goto line?

It opens a new VSCode window (not expected as -r is reuse a current one) with two tabs with file cli.js and the file under %f. Then the goto line is no working as when I click on the file tab the cursor is not on the proper line.

As I said I'm using VSCode in portable mode.

4. Important thing: do not open SumatraPDF externally. Close all SumatraPDF and let vscode open it for you.

Maybe that was the error. I was opening SumatraPDF and then using Ctl+Alt+j for forwarding. If I open it with Crtl+alt+j directly, it works!!!

Thanks a lot.

@qcts33
soft VS Code\Code.exe" "C:\Program Files\Microsoft VS Code\resourcesapp\outcli.js" -g "%f":"%l"`

This works for me too!
However, every time it'll open a new window for VSCode. Do you have any clues to solve this problem?

Well, I've fixed this by myself for the following settings:

"latex-workshop.view.pdf.external.synctex": {
  "command": "C:\\Program Files (x86)\\SumatraPDF\\SumatraPDF.exe",
  "args": [
    "-forward-search",
    "%TEX%",
    "%LINE%",
    "-reuse-instance",
    "-inverse-search",
    "\"C:\\Users\\huangchencheng\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" \"C:\\Users\\huangchencheng\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"",
    "%PDF%"
  ]
},

-r flag here means "reusing last active window". See here.

This workd fine before, but for the newest latexworkshop, the synctex would take extremely long (minutes) to open up the sumatra window. Is it just me? I've tried uninstall and re-install.

@clsmt I also meet the problem in the newest latex-workshop 8.1.0 version,have you solved the problem?

@clsmt The latest version, 8.1.0 is pretty quick wrt sync.

@YouSenRong The settings for external viewer has changed. Please see wiki

Ensure you the following four settings configured correctly

    "latex-workshop.view.pdf.external.synctex.command"
    "latex-workshop.view.pdf.external.synctex.args"
    "latex-workshop.view.pdf.external.viewer.command"
    "latex-workshop.view.pdf.external.viewer.args"

@clsmt I also meet the problem in the newest latex-workshop 8.1.0 version,have you solved the problem?

If you follow my original method, it still works for me (start SumatraPDF separately, not by VSCode).

I did added the lines in settings.json and it worked for forward-searching when I use Ctrl+Alt+J in VSCode but it doesn't work for SumatraPDF. When I double click the pdf it does nothing but I configured the inverse parameters. It does respond the action because my mouse shows loading status for a second but nothing happens.

@clsmt @kman0 Thank you. I may solve the problem in some way as the following configuration in latex-workshop 8.1.0.
"latex-workshop.view.pdf.viewer": "external",

"latex-workshop.view.pdf.ref.viewer":"external",

"latex-workshop.view.pdf.external.viewer.command": "D:/Program Files/SumatraPDF/SumatraPDF.exe",

"latex-workshop.view.pdf.external.viewer.args": [
"%PDF%"
],

"latex-workshop.view.pdf.external.synctex.command":"D:/Program Files/SumatraPDF/SumatraPDF.exe",

"latex-workshop.view.pdf.external.synctex.args":[
"-forward-search",
"%LINE%",
"%PDF%",
"%TEX%"
],
@FrankSFLYS the configuration above may help.

@clsmt @kman0 Thank you. I may solve the problem in some way as the following configuration in latex-workshop 8.1.0.
"latex-workshop.view.pdf.viewer": "external",

"latex-workshop.view.pdf.ref.viewer":"external",

"latex-workshop.view.pdf.external.viewer.command": "D:/Program Files/SumatraPDF/SumatraPDF.exe",

"latex-workshop.view.pdf.external.viewer.args": [
"%PDF%"
],

"latex-workshop.view.pdf.external.synctex.command":"D:/Program Files/SumatraPDF/SumatraPDF.exe",

"latex-workshop.view.pdf.external.synctex.args":[
"-forward-search",
"%LINE%",
"%PDF%",
"%TEX%"
],
@FrankSFLYS the configuration above may help.

Oh Thanks. It worked strangely after I used Ctrl+Alt+J to open SumatraPDF instead of using the latex-workshop opening button.

I can confirm that the inverse search doesn't work if the PDF is opened with the LW PDF button.
If you open the PDF independently it works.


I also notice that, the SumatraPDF instance fired up by VS Code will also exit, if you close VS Code. Are they connected somehow? How to get around this problem?

Currently, this extension uses this code to open SumatraPDF

https://github.com/James-Yu/LaTeX-Workshop/blob/59f08e3f8886e53ccbf1ad5b33d471bdea1ba4e9/src/components/viewer.ts#L174

According to the Node docs, it probably should be

const subprocess = cp.spawn(command, args, {cwd: path.dirname(sourceFile), detached: true, stdio: 'ignore'})
subprocess.unref()

Someone can have a try.


I don't understand why the SumatraPDF instance initiated by VS Code can't perform inverse search, but the independent ones can.

I'm not sure but I suspect it may be related to the environment variables. (see https://github.com/microsoft/vscode/issues/76673#issuecomment-509521524)

I have managed to fix it so that inverse search works no matter how Sumatra is opened (independently, using Ctrl+Alt+V, or Ctrl+Alt+J).

As well as setting the VSCode options as described above, in the SumatraPDF options, you need to set the inverse search command line to:

C:\Users\<name>\AppData\Local\Programs\Microsoft VS Code\Code.exe "C:\Users\<name>\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l"

I tried this setup. But Sumatra keeps telling me "Cannot start inverse search command. Please check the command line in settings". How should I deal with this? Or is there any way to undock the internal viewer so that I don't need to deal with these stupid Sumatra things at all?

I have managed to fix it so that inverse search works no matter how Sumatra is opened (independently, using Ctrl+Alt+V, or Ctrl+Alt+J).

As well as setting the VSCode options as described above, in the SumatraPDF options, you need to set the inverse search command line to:

C:\Users\<name>\AppData\Local\Programs\Microsoft VS Code\Code.exe "C:\Users\<name>\AppData\Local\Programs\Microsoft VS Code\resources\app\out\cli.js" -r -g "%f:%l"

Choose browser at latex-workshop.view.pdf.viewer. See https://github.com/James-Yu/LaTeX-Workshop/wiki/View#latex-workshopviewpdfviewer

For those also looking for the solution, here it is:

  "latex-workshop.view.pdf.external.synctex.command": "SumatraPDF",
  "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\" -r -g \"%f:%l\"",
      "%PDF%",
    ],
Was this page helpful?
0 / 5 - 0 ratings

Related issues

drdebmath picture drdebmath  ·  4Comments

domenicozambella picture domenicozambella  ·  4Comments

seanmcbreen picture seanmcbreen  ·  4Comments

tillahoffmann picture tillahoffmann  ·  6Comments

s-ilic picture s-ilic  ·  3Comments