Java를 사용하여 LaTeX 문서에 그래픽 삽입
Aspose.TeX for Java를 사용하여 고해상도 래스터 이미지(PNG, JPEG, BMP)와 벡터 그림(EPS, PDF)을 LaTeX 문서에 삽입합니다. graphicx와 같은 네이티브 LaTeX 패키지를 활용하고 입력 작업 디렉터리를 구성하면 개발자는 차트, 그림, 사진 및 벡터 그래픽이 포함된 문서를 원활하게 컴파일하여 출판에 적합한 PDF, XPS 및 이미지 출력을 생성할 수 있습니다.
그래픽 요소가 포함된 LaTeX 문서를 컴파일할 때 Aspose.TeX for Java는 구성된 IInputWorkingDirectory를 기준으로 참조된 이미지 파일을 확인합니다.
- 사진, 래스터 스크린샷 및 웹 그래픽의 경우
\usepackage{graphicx}-\includegraphics{figure.png}명령을 사용하여 삽입하는 PNG/JPEG 형식을 선택하세요. - 고정밀 벡터 다이어그램과 CAD/과학용 플롯의 경우
\usepackage{graphicx}-\includegraphics{diagram.eps}명령을 사용하여 삽입하는 EPS 형식을 선택하세요. - 레거시 비트맵 이미지를 삽입하려면
\usepackage{graphicx}-\includegraphics{image.bmp}명령을 사용하여 삽입하는 BMP/TIFF 형식을 선택하세요.
LaTeX에 래스터 이미지(PNG/JPEG) 삽입
표준 래스터 이미지를 삽입하려면 LaTeX 문서의 프리앰블에 graphicx 패키지를 포함하고 \includegraphics{}에 대상 이미지 파일 이름을 지정합니다.
import com.aspose.tex.*;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
public class EmbedRasterGraphicsInLaTeX {
public static void main(String[] args) throws Exception {
// Create LaTeX document string referencing an image file 'sample.png'
String latexCode = "\\documentclass{article}\n" +
"\\usepackage{graphicx}\n" +
"\\begin{document}\n" +
"\\section{Graphics Rendering Demo}\n" +
"Here is an embedded raster image:\n\n" +
"\\begin{figure}[h]\n" +
" \\centering\n" +
" \\includegraphics[width=0.5\\textwidth]{sample.png}\n" +
" \\caption{Sample PNG Image in LaTeX}\n" +
"\\end{figure}\n" +
"\\end{document}";
ByteArrayInputStream inputStream = new ByteArrayInputStream(latexCode.getBytes(StandardCharsets.UTF_8));
// Initialize conversion options for ObjectLaTeX
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Set input working directory where 'sample.png' is located
options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/Images/"));
// Set output working directory for compiled PDF
options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));
// Execute TeX job
TeXJob job = new TeXJob(inputStream, new PdfDevice(), options);
job.run();
System.out.println("LaTeX document with embedded PNG successfully compiled to PDF.");
}
}LaTeX에 벡터 그래픽(EPS) 삽입
Aspose.TeX는 ObjectLaTeX 형식 엔진을 통해 컴파일할 때 Encapsulated PostScript(.eps) 그래픽을 네이티브 방식으로 처리합니다.
import com.aspose.tex.*;
import java.io.IOException;
public class EmbedEPSGraphicsInLaTeX {
public static void main(String[] args) throws IOException {
// Initialize options for LaTeX format engine
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Configure directory paths containing .tex source and .eps graphics
options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/SourceWithEPS/"));
options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));
// Run job for 'document_with_eps.tex' containing \includegraphics{vector_chart.eps}
TeXJob job = new TeXJob("document_with_eps", new PdfDevice(), options);
job.run();
System.out.println("LaTeX document with vector EPS figure compiled successfully.");
}
}ZIP 아카이브에서 그래픽 리소스 로드
TeX 소스 파일, 매크로 패키지 및 동적 이미지를 하나의 .zip 아카이브로 패키징하고 Aspose.TeX가 메모리 또는 압축 저장소에서 그래픽 종속성을 직접 확인하도록 지정합니다.
import com.aspose.tex.*;
import java.io.FileInputStream;
public class EmbedGraphicsFromZip {
public static void main(String[] args) throws Exception {
// Initialize LaTeX engine options
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Open input ZIP archive containing 'main.tex' and 'figure.png'
FileInputStream zipStream = new FileInputStream("C:/TeXProject/ProjectWithAssets.zip");
options.setInputWorkingDirectory(new InputZipDirectory(zipStream, "src"));
// Define output destination
options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));
// Run compilation job
TeXJob job = new TeXJob("main", new PdfDevice(), options);
job.run();
zipStream.close();
System.out.println("Compiled LaTeX document with images embedded directly from ZIP file.");
}
}설치 및 설정
Maven을 사용하여 Aspose.TeX for Java를 프로젝트에 통합합니다:
Package Manager Console Command
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-tex</artifactId>
<version>Latest</version>
</dependency>
FAQ
1. TeXOptions에서 작업 이름을 지정하지 않으면 어떻게 되나요?
setJobName()을 통해 작업 이름을 명시적으로 구성하지 않은 경우 Aspose.TeX는 기본적으로 주 입력 파일 이름을 사용합니다(예: main.tex은 main.pdf 및 main.log를 생성합니다).
2. 컴파일 .log 파일 생성을 완전히 비활성화하려면 어떻게 해야 하나요?
TeXJob을 생성하고 실행하기 전에 options.setLogOutput(false)를 설정합니다. 이렇게 하면 엔진이 디스크나 출력 작업 디렉터리에 보조 로그 파일을 생성하지 않습니다.
3. 서버/headless 환경에서 TeX 터미널 메시지를 캡처할 수 있나요?
예. 사용자 지정 OutputMemoryTerminal을 할당하거나 IOutputTerminal을 구현하면 모든 터미널 stdout/stderr 메시지를 서버의 엔터프라이즈 로깅 시스템(예Log4j 또는 SLF4J)으로 라우팅할 수 있습니다.