Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/7f54d335270200ebf5987d071bad028a to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/7f54d335270200ebf5987d071bad028a to your computer and use it in GitHub Desktop.
Advanced TeX features: job names, terminal output, LaTeX repair, base64 images with .NET

Aspose.TeX for .NET - Advanced Features Examples

This folder contains advanced examples demonstrating powerful features of Aspose.TeX for .NET:

🎯 Examples Included

1. Overridden Job Name with Terminal Output to Disk

  • Override the default TeX job name
  • Write terminal output to a file in the output directory
  • Uses file system for input/output

2. Overridden Job Name with Terminal Output to ZIP

  • Override the TeX job name
  • Write terminal output to a ZIP archive
  • Uses ZIP archives for both input and output

3. LaTeX Repairer

  • Automatically repair invalid LaTeX files
  • Guess missing packages using a callback
  • Fix undefined commands and environments

4. LaTeX with Embedded Base64 Image

  • Convert LaTeX with embedded base64-encoded images
  • Enable shell command execution for image processing
  • Output to PDF format

📚 Key Features Demonstrated

  • Job name customization
  • Terminal output capture
  • LaTeX error repair and package guessing
  • Base64 image embedding
  • Shell mode configuration

🚀 Usage

Each test method is self-contained and can be run independently. The examples show advanced TeX typesetting scenarios for professional document processing.

Aspose.TeX for .NET – Advanced Features Examples
// Convert LaTeX with embedded base64-encoded images
// Learn more: https://docs.aspose.com/tex/net/latex-embedded-graphics/
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
// Initialize the options for saving in PDF format.
options.SaveOptions = new PdfSaveOptions();
// Enable the shell command execution.
options.ShellMode = ShellMode.ShellRestricted;
// Run LaTeX to PDF conversion.
new TeXJob(Path.Combine(DataDir, "embedded-base64-image.tex"), new PdfDevice(), options).Run();
// Repair invalid LaTeX file and guess missing packages
// Learn more: https://docs.aspose.com/tex/net/latex-repair/
// Create repair options.
LaTeXRepairerOptions options = new LaTeXRepairerOptions();
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
// Specify a file system working directory for the required input.
// The directory containing packages may be located anywhere.
options.RequiredInputDirectory = new InputFileSystemDirectory(Path.Combine(DataDir, "packages"));
// Specify the callback class to externally guess packages required for undefined commands or environments.
options.GuessPackageCallback = new PackageGuesser();
// Run the repair process.
new LaTeXRepairer(Path.Combine(DataDir, "invalid-latex.tex"), options).Run();
// Override job name and write terminal output to disk
// Learn more: https://docs.aspose.com/tex/net/aspose-tex-output/
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name. Otherwise, the first argument of the TeXJob constructor will be taken as a job name.
options.JobName = "overridden-job-name";
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(DataDir);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Run the job.
TeXJob job = new TeXJob("hello-world", new XpsDevice(), options);
job.Run();
// Override job name and write terminal output to ZIP archive
// Learn more: https://docs.aspose.com/tex/net/aspose-tex-output/
// Open a stream on a ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(DataDir, "zip-in.zip"), FileMode.Open))
// Open a stream on a ZIP archive that will serve as the output working directory.
using (Stream outZipStream = File.Open(Path.Combine(OutputDir, "terminal-out-to-zip.zip"), FileMode.Create))
{
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name.
options.JobName = "terminal-output-to-zip";
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
// Specify a ZIP archive working directory for the output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Define the saving options.
options.SaveOptions = new PdfSaveOptions();
// Run the job.
new TeXJob("hello-world", new PdfDevice(), options).Run();
// Finalize output ZIP archive.
((OutputZipDirectory)options.OutputWorkingDirectory).Finish();
}
// The callback class to externally guess packages required for undefined commands or environments.
public class PackageGuesser : IGuessPackageCallback
{
private Dictionary<string, string> _map = new Dictionary<string, string>();
public PackageGuesser()
{
_map.Add("lhead", "fancyhdr"); // Defines the mapping between the \lhead command and the fancyhdr package.
}
public string GuessPackage(string commandName, bool isEnvironment)
{
string packageName;
if (!isEnvironment)
{
_map.TryGetValue(commandName, out packageName);
return packageName ?? ""; // It's better to return an empty string to avoid consequent calls for the same command name.
}
// Some code for environments
// ...
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment