\usepackage{claude}
اجمع بين Anthropic Python SDK وFormaTeX لإنشاء ملفات PDF احترافية بالكامل بالذكاء الاصطناعي. يكتب Claude شيفرة LaTeX ويجمّعها FormaTeX، أي استدعاءان لواجهة API وملف PDF واحد.
\section{لماذا Claude}
يكتب Claude شيفرة LaTeX صحيحة
يتميز Claude بإنتاج شيفرة LaTeX سليمة نحويًا مع فئات المستندات والحزم والرموز الرياضية المناسبة.
يجمّع FormaTeX بموثوقية
يشغّل FormaTeX توزيعة TeX Live كاملة على الخادم. يتوفر pdflatex وxelatex في كل خطة، ويتوفر lualatex وlatexmk في خطة Pro وما فوقها، وكل ذلك عبر استدعاء REST واحد.
دون تثبيت TeX محليًا
لا حاجة إلى إعداد TeX Live على جهازك أو خادمك. التجميع مُدار بالكامل، مع بيئات معزولة مؤقتة لكل طلب.
مخرجات بمستوى الإنتاج
احصل على ملفات PDF بدقة عالية مع تقطيع صحيح للكلمات وضبط للتباعد بين الحروف وعرض سليم للخطوط، وبالجودة نفسها التي يوفرها تثبيت LaTeX محلي.
\section{الخطوة 1}
أرسل إلى Claude رسالة نظام تطلب منه الرد بشيفرة LaTeX الخام فقط. تُعدّ رسالة system أساسية: فبدونها قد يغلّف Claude المخرجات بأسوار markdown أو يضيف نصًا توضيحيًا.
import anthropic
client = anthropic.Anthropic(api_key="your-anthropic-key")
SYSTEM_PROMPT = """You are a LaTeX expert. When asked to create a document,
respond ONLY with valid LaTeX source code — no markdown fences, no explanation.
Always include \\documentclass, preamble, and \\begin{document}...\\end{document}."""
def generate_latex(prompt: str) -> str:
"""Ask Claude to write a complete LaTeX document."""
message = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=4096,
system=SYSTEM_PROMPT,
messages=[{"role": "user", "content": prompt}],
)
return message.content[0].text\section{الخطوة 2}
مرّر سلسلة LaTeX التي أعادها Claude مباشرةً إلى نقطة نهاية التجميع في FormaTeX. الاستجابة عبارة عن بايتات PDF خام، احفظها على القرص أو أرسلها إلى العميل.
import requests
FORMATEX_API_KEY = "your-formatex-key"
FORMATEX_URL = "https://api.formatex.io/api/v1/compile"
def compile_to_pdf(latex_source: str, engine: str = "pdflatex") -> bytes:
"""Send LaTeX source to FormaTeX and return PDF bytes."""
response = requests.post(
FORMATEX_URL,
headers={
"X-API-Key": FORMATEX_API_KEY,
"Content-Type": "application/json",
},
json={"latex": latex_source, "engine": engine},
timeout=30,
)
response.raise_for_status()
return response.content\section{مثال كامل}
يجمع النص البرمجي الكامل من البداية إلى النهاية بين الخطوتين. استدعِ generate_and_compile بأي طلب بلغة طبيعية لتحصل على ملف PDF على القرص.
import anthropic
import requests
anthropic_client = anthropic.Anthropic(api_key="your-anthropic-key")
FORMATEX_URL = "https://api.formatex.io/api/v1/compile"
FORMATEX_API_KEY = "your-formatex-key"
SYSTEM_PROMPT = """You are a LaTeX expert. Respond ONLY with valid LaTeX source.
Always include \\documentclass, full preamble, and \\begin{document}...\\end{document}."""
def generate_and_compile(prompt: str, engine: str = "pdflatex") -> bytes:
# Step 1: Claude writes the LaTeX
message = anthropic_client.messages.create(
model="claude-sonnet-5-5",
max_tokens=4096,
system=SYSTEM_PROMPT,
messages=[{"role": "user", "content": prompt}],
)
latex_source = message.content[0].text
# Step 2: FormaTeX compiles to PDF
response = requests.post(
FORMATEX_URL,
headers={
"X-API-Key": FORMATEX_API_KEY,
"Content-Type": "application/json",
},
json={"latex": latex_source, "engine": engine},
timeout=30,
)
response.raise_for_status()
return response.content
# Generate a research summary PDF
pdf_bytes = generate_and_compile(
"Write a 2-page LaTeX article summarising transformer architecture, "
"including the self-attention equation and a simple diagram using TikZ."
)
with open("transformer_summary.pdf", "wb") as f:
f.write(pdf_bytes)
print("PDF saved to transformer_summary.pdf")\section{Related}
\end{claude}
احصل على مفتاح FormaTeX API، وانسخ المثال الكامل أعلاه، وأطلق ملفات PDF المولَّدة بالذكاء الاصطناعي في دقائق.