如何在左侧相应的 EPS 更改时自动更新右侧查看的 PDF?

问题描述 投票:0回答:1

我正在使用 Visual Studio Code 来学习 PostScript 语言。

我使用以下

task.json
将EPS转换为PDF。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "EPS",
            "command": "ps2pdf",
            "args": [
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

我的 EPS 是

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 10 10 300 300

%%BeginProlog
/inch {72 mul} def
/cm {2.54 div inch} def
%%EndProlog


newpath
    1 cm 1 cm moveto
    9 cm 0 cm rlineto
    0 cm  9 cm rlineto
    -9 cm 0 cm rlineto
    9 cm 9 cm rlineto
closepath
5 setlinewidth
stroke
showpage
%%EOF

问题

如何在左侧相应的 EPS 更改时自动更新右侧查看的 PDF?

visual-studio-code postscript vscode-tasks
1个回答
0
投票

我相信你在过去的两年里已经弄清楚了一些事情——但是对于像我一样偶然发现这一点的人来说,这里是我如何使用 TypeScript 在 LaTeX 中编写的简历中实现这一点的片段——我我确信可以将其应用于 PostScript:

package.json:

{
  "name": "resume",
  "version": "1.0.0",
  "scripts": {
    "watch": "ts-node watch.ts",
    "generate": "pdflatex -jobname=resume resume.tex"
  },
  "devDependencies": {
    "@types/node": "^20.5.1",
    "chokidar": "^3.5.3",
    "ts-node": "^10.9.1"
  }
}

watch.ts

import { watch } from "chokidar";
import { exec } from "child_process";

// Files to watch for change
const watchFiles: string[] = [
  'resume.tex',
  '_header.tex',
  'TLCresume.sty',
  'sections/**/*.tex',
]

// Watch for changes in the files specified above
const watcher = watch(watchFiles)

function buildPDF() {
  // Execute the 'generate' script in package.json
  // This will run the 'pdflatex' command to generate the PDF
  exec('npm run generate', (err, stdout, stderr) => {
    if (err) {
      // node couldn't execute the command
      return;
    }

    // the *entire* stdout and stderr (buffered)
    console.log("PDF generated!")
  });
}

// Build PDF once at the start
buildPDF();
let lastBuildTime: number = Date.now();

watcher.on('all', (event, path) => {
  if (Date.now() - lastBuildTime < 1000) {
    // Don't build PDF if it was just built less than a second ago
    return;
  } else {
    console.log(`${event} @ ${path}`);
  }

  buildPDF();
  lastBuildTime = Date.now();
});

然后:

  1. 使用 npm
    npm install
    安装 package.json 中的依赖项。
  2. package.json
    中的“生成”脚本更改为适合生成 PostScript PDF 的任何命令。
  3. 更改
    watchFiles
    数组以包含 PostScript 文件的路径/正则表达式。
  4. 最后,每当您保存文件时,运行
    npm run watch
    即可在 VSCode 中刷新 PDF!
© www.soinside.com 2019 - 2024. All rights reserved.