How to use the command "pandoc" (with examples)
Pandoc is a powerful command-line tool for converting documents between various formats. It supports a wide array of input and output formats, making it highly versatile and useful for dealing with different document types. In this article, we will explore several use cases of the pandoc
command with code examples, motivations, explanations, and example outputs.
Use Case 1: Convert file to PDF
pandoc input.md -o output.pdf
Motivation
Converting Markdown files to PDF is a common task for creating printable documents or sharing formatted content. By using Pandoc, we can easily convert Markdown files to PDF with just one command.
Explanation
input.md
: Specifies the path to the input file in Markdown format.-o output.pdf
: Specifies the output file path and the desired format, in this case, PDF.
Example Output
The input Markdown file input.md
will be converted to a PDF file named output.pdf
, preserving the formatting and content of the original Markdown document.
Use Case 2: Force conversion to a specific format
pandoc input.docx --to gfm -o output.md
Motivation
Sometimes we need to convert files to a specific output format that may not be the default conversion choice. By using the --to
option followed by the desired output format, we can override the default behavior and convert files accordingly.
Explanation
input.docx
: Specifies the path to the input file in DOCX format.--to gfm
: Sets the output format to GitHub-flavored Markdown (GFM).-o output.md
: Specifies the output file path and the desired format, in this case, Markdown.
Example Output
The input DOCX file input.docx
will be converted to a Markdown file named output.md
, using the GitHub-flavored Markdown format.
Use Case 3: Convert to a standalone file with appropriate headers/footers
pandoc input.md -s -o output.tex
Motivation
When converting documents to certain formats like LaTeX or HTML, we may need to include additional headers and footers to create standalone files that can be rendered correctly. By using the -s
option, Pandoc will generate output files with the necessary headers and footers for the specified format.
Explanation
input.md
: Specifies the path to the input file in Markdown format.-s
: Enables standalone mode, which adds appropriate headers and footers for the output format.-o output.tex
: Specifies the output file path and the desired format, in this case, LaTeX.
Example Output
The input Markdown file input.md
will be converted to a standalone LaTeX file named output.tex
. The resulting file will include the necessary LaTeX document class, preamble, and other components for proper rendering.
Use Case 4: List all supported input formats
pandoc --list-input-formats
Motivation
It’s crucial to know the available input formats that Pandoc supports to ensure the compatibility of our input documents. By utilizing the --list-input-formats
option, we can obtain a comprehensive list of the supported input formats.
Explanation
--list-input-formats
: Displays a list of all the input formats supported by Pandoc.
Example Output
Running the command will output a list of all the supported input formats, including commonly used formats like Markdown, HTML, DOCX, and LaTeX, as well as more exotic formats such as Jupyter notebooks and MediaWiki.
Use Case 5: List all supported output formats
pandoc --list-output-formats
Motivation
Similar to the previous use case, it is crucial to know the available output formats that Pandoc supports. By using the --list-output-formats
option, we can obtain a comprehensive list of the supported output formats.
Explanation
--list-output-formats
: Displays a list of all the output formats supported by Pandoc.
Example Output
Executing the command will output a list of all the supported output formats, including popular ones like HTML, PDF, DOCX, and LaTeX, as well as lesser-known formats such as EPUB, AsciiDoc, and ODT.
Conclusion
In this article, we explored five different use cases of the Pandoc command. We learned how to convert files to PDF, force conversion to a specific format, generate standalone files, and list all supported input and output formats. By mastering these capabilities of Pandoc, you can efficiently convert documents between various formats, making it a valuable tool for document conversion tasks.