James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.56k stars 524 forks source link

Additional tool chain process: latex + dvisvgm #464

Closed sabicalija closed 6 years ago

sabicalija commented 6 years ago

Description

I would like to add an additional build process to latex workshop, that I could use globally when working on latex files.

Today I figured out, that I can create svg with latex + dvisvgm. I want those two steps when working with TikZ, globally available in vscode.

For instance, suppose following tex file:

drawing.tex

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
   \begin{tikzpicture}
      \draw (0,0) rectangle ++(3,2);
   \end{tikzpicture}
\end{document}

With following two steps, one can create an svg out of this tex.

$ latex drawing.tex
$ dvisvgm drawing.dvi

The result is an svg named drawing.svg.

Is it possible to add additional build processes and instruct Latex Workshop to select one of those available, maybe per file extension, or with "Magic comments"??

Would be great if one could customize the steps necessary.

James-Yu commented 6 years ago

It's a popular request. However, there's no current plan to implement it. When you have two toolchains, people will ask for more.

A good alternative is vscode tasks. You can define a series of command in a task. I have no previous experience in it but I believe it fits your request.

sabicalija commented 6 years ago

Thanks for the tips. Anyway, I do understand your decision. I was hoping someone will know how to deal with it, or it would inspire someone to implement such a feature. :crossed_fingers:

However, I've setup a tasks which does exactly what I was describing above. The main problem with it was, that those tasks can't be configured globally, unfortunately.

Actually, I had following approach in mind:

image

My suggestion is to create an additional "recipes" entry, which allows to select from a list of such "recipes". A user would than configure them similarly to the "toolchain" customization, but within an array.

For instance:

"latex-workshop.latex.recipes" : [
  {
    "name": "latex",
    "commands": [
      {
        "command": "latex",
        "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-output-directory=",
          "%DOC%"
        ]
      }
    ]
  },
  {
    "name": "latex + bibtex + latex",
    "commands": [
      {
        "command": "latex",
        "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-output-directory=",
          "%DOC%"
        ]
      },
      {
        "command": "bibtex",
        "args": [
          "%DOC%"
        ]
      },
      {
        "command": "latex",
        "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-output-directory=",
          "%DOC%"
        ]
      }
    ]
  },
  {
    "name": "pdflatex + bibtex",
    "commands": [
      {
        "command": "pdflatex",
        "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-output-directory=",
          "%DOC%"
        ]
      },
      {
        "command": "latex",
        "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-output-directory=",
          "%DOC%"
        ]
      }
    ]
  }
]
James-Yu commented 6 years ago

That's actually a really good idea. Let me think of how to implement it.

ghost commented 6 years ago

Hi, i'm trying to implement this. Here is prototype: https://github.com/project-pp/LaTeX-Workshop/tree/recipes @sabicalija Could you review the impression of my imprementation (if possible). @James-Yu If you are interested in it, pls review about backward-compatibility (and its structure of implementation). If the good review received, I'll opne pull-request and trying to improve it. Thanks!

sabicalija commented 6 years ago

@project-pp: sure, thank you for the work.

I'm not sure, if I'm testing it right. I've downloaded your repository and placed it in the vsocde extensions folder. I've renamed it to James-Yu.latex-workshop-3.14.0/ and started vscode.

When I try to open "Latex Workshop: All actions", I only get an error message telling me:

command 'latex-workshop.actions' not found

Is there a way to test it, or did you ask me only to review the commits?

ghost commented 6 years ago

sorry branch replaced


@sabicalija try this:

$ git clone https://github.com/project-pp/LaTeX-Workshop.git
$ cd LaTeX-Workshop
$ git checkout recipes
$ npm i -D -S
$ code .

then press F5 in your VSCode to Launch the extension with my implementation.

ghost commented 6 years ago

sample usage (current version):

"latex-workshop.latex.recipes": {
        "@makeindex": [ // key starts with '@' would be ignored in the selection list
            {
                "command": "makeindex",
                "args": [
                    "%DOCFILE%"
                ]
            }
        ],
        "@XeLaTeX": [
            {
                "command": "xelatex",
                "args": [
                    "%DOC%"
                ]
            }
        ],
        "xelatex with makeindex": [
            { "do": "@XeLaTeX" }, // you can use "do"; recipe reference
            { "do": "@makeindex" },
            // { "do": "@XeLaTeX" }
            // of course you can use normal toolchain command item
            {
                "command": "xelatex",
                "args": [
                    "%DOC%"
                ]
            }
        ]
    },

test

sabicalija commented 6 years ago

Works like a charm!

Great idea with the recipe references! I'd missed exactly this feature, but I didn't come up with such an elegant solution. Great work!

I've created following recipes:

"latex-workshop.latex.recipes": 
    {
      "@latex": [
        {
          "command": "latex",
          "args": [
            "-output-directory=build",
            "%DOCFILE%"
          ]
        }
      ],
      "@dvisvgm" : [
        {
          "command": "dvisvgm",
          "args": [
            "build/%DOCFILE%"
          ]
        },
      ],
      "svg": [
        { "do": "@latex" },
        { "do": "@dvisvgm" }
      ]
    }

and it worked instantly! Thank you so much! :relieved:

I've one more question. When I try to add a simple mv command, like

...
 "svg": [
        { "do": "@latex" },
        { "do": "@dvisvgm" },
        {
           "command": "mv",
           "args": "%DOCFILE%.svg build/
         }
      ]
...

the recipe results in and error and I'm getting following errors in the Latex Workshop output:

[22:58:22] Toolchain step 3: mv, demo.svg build/
[22:58:22] Toolchain returns with error: 1/null.

I'm wondering, if this could be caused by the comma at step 3:. Do you have an idea what might cause a problem or how to solve it?

ghost commented 6 years ago

Actually it is not related to recipes. LaTeX-Workshop would invoke each toolchain command as non-shell mode (see builder.ts line 66, refer node doc). User-space solution is to invoke your command via shell manually. something like this (not tested):

{
   "command": "bash",
   "args": [
       "-lic",
       "mv %DOCFILE%.svg build/"
    ]
}

To declare new option something like shell?: boolean to ToolchainCommand and switch spawn option is possible solution. But I don't have a plan to do this in the current branch. If you really need such a thing, it would be better to post another issue and discuss about it.

James-Yu commented 6 years ago

@project-pp A neat solution! Waiting for your PR. Actually I have a plan to overhaul the current toolchain system, which may be built on your recipe system.

James-Yu commented 6 years ago

Implemented. Will release in the next update.

sabicalija commented 6 years ago

Thanks a lot, once more! Both of you. Just tried the new release. Great work. :v:

EiArDi commented 3 years ago

Try this:

    "latex-workshop.latex.tools": [
    {
        "name": "latex",
        "command": "latex",
        "args": [
            "-synctex=1",
            "-interaction=nonstopmode",
            "-file-line-error",
            "-pdf",
            "-outdir=%OUTDIR%",
            "%DOC%"
        ],
        "env": {}
    },
    {
        "name": "dvisvgm",
        "command": "dvisvgm",
        "args": [
            "%DOC%"
        ],
        "env": {}
    },
    "latex-workshop.latex.recipes": [
    {
        "name": "latex+dvisvgm",
        "tools": [
            "latex",
            "dvisvgm"
        ]
    },
    ]