명령줄을 통해 Mathematica를 PDF로 변환

명령줄을 통해 Mathematica를 PDF로 변환

저는 Linux를 사용하고 있으며 Mathematica 8 Notebook을 PDF로 변환하고 싶습니다.

명령줄에서 변환할 수 있는 방법이 있나요? 변환을 위한 makefile 규칙을 작성하여 많은 항목을 일괄 변환할 수 있도록 하고 싶습니다.

답변1

기본적으로 프런트엔드를 호출하지 않고서는 Mathematica 노트북을 PDF로 변환할 수 있는 방법이 없습니다. 인쇄하거나 변환하려면 먼저 열어야 하며 노트북에서 노트북을 열려는 순진한 시도가 필요합니다.Mathematica 명령줄오류가 발생합니다 FrontEndObject::사용할 수 없음

In[1]:= NotebookOpen["file.nb"]

FrontEndObject::notavail: 
   A front end is not available; certain operations require a front end.

이는 변환을 수행하기 위해 노트북을 만들거나 명령줄에서 프런트엔드를 호출할 수 있음을 의미합니다. 다음은 다음과 같은 형태의 솔루션입니다.Mathematica 스크립트- 노트북이나 패키지 파일로 쉽게 변환할 수 있습니다.

다음 코드를 nb2pdf.

#!/usr/local/bin/MathematicaScript -script

(* Convert Mathematica notebooks to PDFs                              *)
(*   usage: nb2pdf file1.nb file2.nb etc...                           *)
(* outputs: file1.pdf file2.pdf etc...  into the current directoy     *)
(* If called with no filenames, this script                           *)
(*    will convert all notebook files in the current directory        *)

dir = Directory[];
files = {};
expandNb = False; (* Expand all cell groups in the Notebook *)

If[Length[$ScriptCommandLine] > 1, 
  Do[If[FileExistsQ[file], 
    AppendTo[files, file], 
    Print["File " <> file <> " does not exist"]],
    {file, Rest[$ScriptCommandLine]}],
  files = FileNames["*.nb"]];

With[{UFE = UsingFrontEnd},
 Do[nb = UFE@NotebookOpen[FileNameJoin[{dir, file}]];
  If[expandNb, UFE@SelectionMove[nb, All, Notebook]; 
               UFE@FrontEndExecute[FrontEndToken["SelectionOpenAllGroups"]]];
  UFE@NotebookPrint[nb, FileNameJoin[{dir, FileBaseName[file]<>".pdf"}]];
  UFE@NotebookClose[nb], {file, files}]]

답변2

Mathematica 13에서 작동하는 것: https://knanagnostopoulos.blogspot.com/2023/01/convert-many-mathematica-notebooks-to.html

nb2pdf (){ for f in $@;do echo -n "Converting $f to pdf ... "; wolframscript -code nb=\"$f\"';FileConvert[nb, "PDF"];' ;done; } 

nb2pdf *.nb

관련 정보