그래프 삽입 방식: PGF vs PDF/SVG

PGF는 LaTeX 코드로 그래프를 그려 문서와 글꼴이 완벽히 통합되지만, 일부 프로그램에서만 지원합니다. PDF/SVG는 대부분의 프로그램에서 지원하는 벡터 형식으로, PGF만큼은 아니지만 매우 높은 품질을 보장합니다.

1. Matplotlib (pyplot) → PGF

Python의 Matplotlib으로 그린 그래프를 .pgf 파일로 저장하는 방법입니다. text.usetex 설정을 활성화하는 것이 핵심입니다.

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "pgf.texsystem": "xelatex",
    "pgf.preamble": "\\usepackage{kotex}"
})

x = np.linspace(0, 2 * np.pi, 100)
plt.plot(np.sin(x), label="$\\sin(x)$")
plt.title("Sine Wave (사인파)")
plt.savefig("matplotlib_plot.pgf")

2. MATLAB → PGF

MATLAB에서 그린 그래프를 .pgf 파일로 저장합니다. print 명령어에 -dpgf 옵션을 사용합니다.

x = linspace(0, 2*pi, 100);
y = sin(x);
plot(x, y);
title('Sine Wave');
print -dpgf matlab_plot.pgf

3. Plotly → PDF / SVG

Plotly는 PGF를 직접 지원하지 않지만, 고품질 벡터 형식인 PDF나 SVG로 그래프를 내보낼 수 있습니다. PDF로 저장하려면 kaleido 패키지가 필요합니다. (pip install kaleido)

import plotly.graph_objects as go

fig = go.Figure(data=go.Scatter(x=np.arange(10), y=np.arange(10)**2))
fig.update_layout(title_text="Plotly Figure")

# --- PDF 또는 SVG 파일로 저장 ---
# fig.write_image("plotly_plot.svg")
fig.write_image("plotly_plot.pdf")

4. LaTeX 문서에 그래프 포함하기

pgfgraphicx 패키지를 사용하고, 파일 형식에 따라 \input 또는 \includegraphics 명령어로 파일을 불러옵니다.

\documentclass[12pt]{article}
\usepackage{kotex}
\usepackage{pgf}
\usepackage{graphicx}

\begin{document}

% PGF 파일 포함 (Matplotlib, MATLAB)
\begin{figure}[h!]
  \centering
  \input{matplotlib_plot.pgf}
  \caption{Matplotlib PGF 그래프}
\end{figure}

% PDF/SVG 파일 포함 (Plotly)
\begin{figure}[h!]
  \centering
  \includegraphics[width=0.8\textwidth]{plotly_plot.pdf}
  \caption{Plotly PDF 그래프}
\end{figure}

\end{document}