Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save documentprocessing/985a7384d061b5334e60f7f327bec11b to your computer and use it in GitHub Desktop.

Select an option

Save documentprocessing/985a7384d061b5334e60f7f327bec11b to your computer and use it in GitHub Desktop.
Converting Markdown to PDF with LaTeX support in Java
import com.github.davidmoten.pandoc.Pandoc;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Converts academic Markdown (with LaTeX math and citations) to PDF using Pandoc.
* Requires:
* - Pandoc installed (v2.11+)
* - LaTeX distribution (e.g., TeX Live/MiKTeX)
* - Bibliography file (references.bib)
*/
public class AcademicPdfConverter {
public static void main(String[] args) {
// 1. Initialize Pandoc instance
Pandoc pandoc = Pandoc.create();
// 2. Define input/output files
String markdownFile = "paper.md";
String pdfOutput = "output.pdf";
String bibliography = "references.bib";
try {
// 3. Execute conversion with academic formatting flags
pandoc.from("markdown")
.to("pdf")
.option("--pdf-engine=xelatex") // LaTeX engine for Unicode/math support
.option("--citeproc") // Process citations
.option("--bibliography=" + bibliography)
.option("--csl=apa.csl") // APA citation style (optional)
.standalone() // Adds LaTeX header/footer
.execute(Files.newInputStream(Paths.get(markdownFile)),
Files.newOutputStream(Paths.get(pdfOutput)));
System.out.println("PDF generated successfully: " + pdfOutput);
} catch (IOException e) {
System.err.println("Conversion failed: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment