简体   繁体   English

将 SVG 包含到 LaTeX 中

[英]Including SVG into LaTeX

I want to include a svg file in a LaTeX document.我想在 LaTeX 文档中包含一个 svg 文件。

a.svg (given) a.svg(给定)

给定的.svg

a.pdf (goal) a.pdf(目标)

目标.pdf I created the table with OpenOffice, exported it to a pdf-file, imported it into inkscape afterwards and saved it as an svg file.我使用 OpenOffice 创建了表格,将其导出为 pdf 文件,然后将其导入inkscape 并将其另存为 svg 文件。

What I have tried我试过的

Include svg directly直接包含 svg

I tried to include the given svg file directly using the svg package :我尝试使用svg 包直接包含给定的 svg 文件:

\usepackage{svg}
...
\includesvg{img/a}

But it did not compile, because pdflatex did not found the file a.但是它没有编译,因为pdflatex没有找到文件a。 I also tried a.svg but without any success :(我也试过 a.svg 但没有成功:(

Exporting to pdf "on the fly" “即时”导出为 pdf

Following J. Engelen I tried to generate the pdf file using pdflatex with -enable-write18 or --shell-escape but it didn't do anything with the .svg file except throwing an error, that the .pdf_tex file is missing.J. Engelen 之后,我尝试使用带有 -enable-write18 或 --shell-escape 的 pdflatex 生成 pdf 文件,但它对 .svg 文件没有做任何事情,除了抛出错误,即 .pdf_tex 文件丢失。

Export to pdf / pdf_tex before including在包含之前导出到 pdf / pdf_tex

I exported the svg file using inkscape v. 0.92:我使用inkscape v. 0.92导出了svg文件:

inkscape -D -z --file=a.svg --export-pdf=a.pdf --export-latex inkscape -D -z --file=a.svg --export-pdf=a.pdf --export-latex

I got a .pdf file containing the blank table:我得到了一个包含空白表格的 .pdf 文件: 原始pdf文件 And a .pdf_tex file containing the following:以及包含以下内容的 .pdf_tex 文件:

%% Creator: Inkscape inkscape 0.92.1, www.inkscape.org
%% PDF/EPS/PS + LaTeX output extension by Johan Engelen, 2010
%% Accompanies image file 'RLE1.pdf' (pdf, eps, ps)
%%
%% To include the image in your LaTeX document, write
%%   \input{<filename>.pdf_tex}
%%  instead of
%%   \includegraphics{<filename>.pdf}
%% To scale the image, write
%%   \def\svgwidth{<desired width>}
%%   \input{<filename>.pdf_tex}
%%  instead of
%%   \includegraphics[width=<desired width>]{<filename>.pdf}
%%
%% Images with a different path to the parent latex file can
%% be accessed with the `import' package (which may need to be
%% installed) using
%%   \usepackage{import}
%% in the preamble, and then including the image with
%%   \import{<path to file>}{<filename>.pdf_tex}
%% Alternatively, one can specify
%%   \graphicspath{{<path to file>/}}
%% 
%% For more information, please see info/svg-inkscape on CTAN:
%%   http://tug.ctan.org/tex-archive/info/svg-inkscape
%%
\begingroup%
  \makeatletter%
  \providecommand\color[2][]{%
    \errmessage{(Inkscape) Color is used for the text in Inkscape, but the package 'color.sty' is not loaded}%
    \renewcommand\color[2][]{}%
  }%
  \providecommand\transparent[1]{%
    \errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package 'transparent.sty' is not loaded}%
    \renewcommand\transparent[1]{}%
  }%
  \providecommand\rotatebox[2]{#2}%
  \ifx\svgwidth\undefined%
    \setlength{\unitlength}{608.85768187bp}%
    \ifx\svgscale\undefined%
      \relax%
    \else%
      \setlength{\unitlength}{\unitlength * \real{\svgscale}}%
    \fi%
  \else%
    \setlength{\unitlength}{\svgwidth}%
  \fi%
  \global\let\svgwidth\undefined%
  \global\let\svgscale\undefined%
  \makeatother%
  \begin{picture}(1,0.13941476)%
    \put(0.12815803,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{352}}}%
    \put(0.18117534,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{320}}}%
    \put(0.23419265,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{288}}}%
    \put(0.28720997,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{256}}}%
    \put(0.34027656,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{224}}}%
    \put(0.39329387,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{192}}}%
    \put(0.44631119,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{160}}}%
    \put(0.4993285,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{128}}}%
    \put(0.56160906,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{96}}}%
    \put(0.61462638,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{64}}}%
    \put(0.66767654,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{32}}}%
    \put(0.7299571,0.12595718){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0}}}%
    \put(0.12007732,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.17309463,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.22611195,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x3}}}%
    \put(0.27912926,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x2}}}%
    \put(0.33214658,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x2}}}%
    \put(0.38521317,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.43823048,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.49124779,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.54426511,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.59728242,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.65033259,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.7033499,0.10191216){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.39329387,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{192}}}%
    \put(0.44631119,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{160}}}%
    \put(0.4993285,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{128}}}%
    \put(0.56160906,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{96}}}%
    \put(0.61462638,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{64}}}%
    \put(0.66767654,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{32}}}%
    \put(0.7299571,0.03056543){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0}}}%
    \put(0.33214658,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{\textit{0}\textit{x}\textit{2}}}}%
    \put(0.38521317,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.43823048,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{\textit{0}\textit{x}\textit{1}}}}%
    \put(0.49124779,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x3}}}%
    \put(0.54426511,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{\textit{0}\textit{x}\textit{2}}}}%
    \put(0.59728242,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x2}}}%
    \put(0.65033259,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{\textit{0}\textit{x}\textit{7}}}}%
    \put(0.7033499,0.00652041){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{0x1}}}%
    \put(0.04755463,0.10270052){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{…}}}%
    \put(0.15358269,0.00730877){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{…}}}%
    \put(0.82929066,0.10210925){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{unkomprimierte Daten}}}%
    \put(0.90260828,0.05441337){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{RleCompress}}}%
    \put(0.84821134,0.0067175){\color[rgb]{0,0,0}\makebox(0,0)[lb]{\smash{komprimierte Daten}}}%
    \put(0,0){\includegraphics[width=\unitlength,page=44]{img/a.pdf}}%
  \end{picture}%
\endgroup%

I used this file within LaTeX like the following:我在 LaTeX 中使用了这个文件,如下所示:

\begin{figure}
    \centering
    \def\svgwidth{\textwidth}
    \input{a.pdf_tex}    
\end{figure}

The resulting .pdf file from my LaTeX file looks like that:我的 LaTeX 文件生成的 .pdf 文件如下所示: 结果.pdf

As one can see, the italic font style is everything, inkscape exported.如您所见,斜体字体样式就是一切,inkscape 导出。 The font color is not exported at all.根本不导出字体颜色。

The LaTeX font size of the index values should be \\tiny .索引值的 LaTeX 字体大小应为\\tiny Of course I can change the font style manually, but this does not seem like a proper way to me.当然我可以手动更改字体样式,但这对我来说似乎不是正确的方法。

Also, the boxes around the index values behave like the following, when I try to resize them using \\tiny :此外,当我尝试使用\\tiny调整它们的大小时,索引值周围的框的行为如下所示: 奇怪的盒子对齐方式

Does anyone knows, how to proper import svg files, containing text into a LaTeX document while preserving all the font styles and colors and of course the positioning?有谁知道,如何正确导入 svg 文件,将文本包含到 LaTeX 文档中,同时保留所有字体样式和颜色,当然还有定位?

Sincerely真挚地

Strange, the approach you used works perfectly for me, however I get a different pdf_tex file from conversion. 奇怪的是,你使用的方法对我来说非常合适,但是我从转换中得到了一个不同的pdf_tex文件。

b.pdf_tex b.pdf_tex

%% Creator: Inkscape inkscape 0.92.1, www.inkscape.org
%% PDF/EPS/PS + LaTeX output extension by Johan Engelen, 2010
%% Accompanies image file 'b.pdf' (pdf, eps, ps)
%%
%% To include the image in your LaTeX document, write
%%   \input{<filename>.pdf_tex}
%%  instead of
%%   \includegraphics{<filename>.pdf}
%% To scale the image, write
%%   \def\svgwidth{<desired width>}
%%   \input{<filename>.pdf_tex}
%%  instead of
%%   \includegraphics[width=<desired width>]{<filename>.pdf}
%%
%% Images with a different path to the parent latex file can
%% be accessed with the `import' package (which may need to be
%% installed) using
%%   \usepackage{import}
%% in the preamble, and then including the image with
%%   \import{<path to file>}{<filename>.pdf_tex}
%% Alternatively, one can specify
%%   \graphicspath{{<path to file>/}}
%% 
%% For more information, please see info/svg-inkscape on CTAN:
%%   http://tug.ctan.org/tex-archive/info/svg-inkscape
%%
\begingroup%
  \makeatletter%
  \providecommand\color[2][]{%
    \errmessage{(Inkscape) Color is used for the text in Inkscape, but the package 'color.sty' is not loaded}%
    \renewcommand\color[2][]{}%
  }%
  \providecommand\transparent[1]{%
    \errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package 'transparent.sty' is not loaded}%
   \renewcommand\transparent[1]{}%
  }%
  \providecommand\rotatebox[2]{#2}%
  \ifx\svgwidth\undefined%
    \setlength{\unitlength}{798bp}%
    \ifx\svgscale\undefined%
      \relax%
    \else%
      \setlength{\unitlength}{\unitlength * \real{\svgscale}}%
    \fi%
  \else%
    \setlength{\unitlength}{\svgwidth}%
  \fi%
  \global\let\svgwidth\undefined%
  \global\let\svgscale\undefined%
  \makeatother%
  \begin{picture}(1,0.23026316)%
    \put(0,0){\includegraphics[width=\unitlength,page=1]{b.pdf}}%
  \end{picture}%
\endgroup%

test.tex test.tex

\documentclass[10pt]{scrreprt}

\usepackage{graphicx}
\usepackage[T1]{fontenc}

\begin{document}

\begin{figure}
    \centering
    \def\svgwidth{\columnwidth}
    \input{b.pdf_tex}
\end{figure}

\end{document}

Which results in: 结果如下: 工作实例

My Inkscape version is 我的Inkscape版本是

Inkscape 0.92.1 r Inkscape 0.92.1 r

pdfTeX 3.14159265-2.6-1.40.17 (TeX Live 2016/Arch Linux) kpathsea version 6.2.2 pdfTeX 3.14159265-2.6-1.40.17(TeX Live 2016 / Arch Linux)kpathsea版本6.2.2

I realize that this thread is 4 + years old but since I found my way here maybe someone else will.我意识到这个线程已经有 4 年以上的历史了,但既然我在这里找到了自己的方式,也许其他人会这样做。

The syntax inkscape -D -z etc.. is deprecated for newer versions of inkscape and got replaced by:较新版本的inkscape 不推荐使用语法inkscape -D -z 等,取而代之的是:

inkscape --export-filename=yourpdfoutput.pdf yoursvginput.svg

For more details see the man-page for inkscape.有关更多详细信息,请参阅inkscape 的手册页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM