pic2graph Command in Linux



The pic2graph command in Linux converts a PIC diagram into a cropped image. It automatically trims the output image to the smallest bounding box containing non-white pixels. PIC diagrams are visual representations created using the PIC language, a high-level graphics description language designed for programmatically creating simple diagrams.

Table of Contents

Here is a comprehensive guide to the options available with the pic2graph command −

Installation pic2graph Command

The pic2graph command is part of groff package. To use pic2graph command this package must be installed.

To install groff on Ubuntu, Kali Linux, Raspberry Pi OS, Debian, and other Debian-based distributions, use the following command −

sudo apt install groff

To install it on Arch Linux, use the following command −

sudo pacman -S groff

For CentOS, use the command given below −

sudo yum install groff

To install groff on Fedora, use the following command −

sudo dnf install groff

To verify whether the pic2graph command is installed or not, check its version −

pic2graph --version
pic2graph Command in Linux1

Syntax of pic2graph Command

The syntax of the pic2graph command is as follows −

pic2graph [options] [file...]

In the above syntax, the [options] field is used to specify various options to modify the command's behavior. The [file...] field is used to specify one or more files to process.

Note − The pic2graph command relies on the ImageMagick tool for tasks such as setting borders and resizing. Ensure that ImageMagick is installed.

pic2graph Command Options

The options of the pic2graph command are listed below −

Options Description
--help Displays a usage message and exits.
-v, --version Shows version information and exits.
-eqn delimiters Specifies the delimiters for eqn directives. Defaults to $$. An empty string disables eqn processing.
-format output-format Specifies the output format for the image, must be understood by convert. Defaults to PNG.
-unsafe Runs groff in unsafe mode, enabling the PIC command to execute arbitrary Unix shell commands.

Examples of pic2graph Command in Linux

This section demonstrates the usage of the pic2graph command in Linux with examples −

Converting a PIC Diagram to an Image File

To convert a PIC diagram to PNG, use the pic2graph command in the following way −

echo 'box "Hello"' | pic2graph > output.png
pic2graph Command in Linux2

The above command creates a box with hello text inside and saves it as the output.png file name in the current working directory.

pic2graph Command in Linux3

Note that the input PIC code for pic2graph should not include the .PS and .PE (or .PF/.PY) macros usually used to enclose PIC diagrams in groff documents.

Converting a PIC Diagram to a Custom Format

By default, the pic2graph command converts the PIC diagram to PNG format. To specify a different format, use the -format option −

echo 'circle radius 3' | pic2graph -format jpeg > output.jpg
pic2graph Command in Linux4

The output image is as follows −

pic2graph Command in Linux5

Specifying Custom Delimiters for Equations

By default, the equations are wrapped in $$ delimiters as given in the following command −

echo 'box "E = $$mc^2$$"' | pic2graph > output.png

To change the delimiters, use the -eqn options with delimiters as a string. For example, to specify { and } as a delimiter, use the pic2graph command in the following way −

echo 'box "E = {mc^2}"' | pic2graph -eqn "{}" > output.png

If the -eqn option is not used in the above command, the output will contain the { and } as well, considering it part of the text.

Using Unsafe Mode

By default, the pic2graph command forbids using arbitrary shell commands. To run the Unix shell commands with pic2graph, use the -unsafe option. For example, to set the border, use the following command −

echo 'box "Hello"' | pic2graph -unsafe -format png -border 10x10 > output.png

Similarly, to resize the image, use the -resize option −

echo 'box "Hello"' | pic2graph -unsafe -format png -border 10x10 -resize 400x400 > output.png
pic2graph Command in Linux6

Displaying Usage Help

To display the usage help, use the --help option −

pic2graph --help

Conclusion

The pic2graph command in Linux is a powerful tool for converting PIC diagrams into cropped image files, commonly in PNG format. It supports various options for customization, including output format, equation delimiters, and the use of unsafe mode for advanced shell commands. Installation requires the groff package and ImageMagick for image processing.

In this tutorial, we covered the pic2graph command, its installation, dependencies, syntax, options, and usage in Linux with examples.

Advertisements