按文本大小缩放 SVG

按文本大小缩放 SVG

我有这个文件

\documentclass{article}
\usepackage{svg}
\newcommand\mtg[1]{%
  \includesvg[height=8px]{#1.svg}
}

\begin{document}
This magic card costs \mtg{R} mana
\end{document}

哪里R.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><g fill="none"><circle fill="#E49977" cx="50" cy="50" r="50"/><path d="M91.965 66.617c-3.736 8.912-11.16 13.367-22.275 13.367-2.037 0-4.246.254-6.621.762-3.564.764-5.346 1.828-5.346 3.186 0 .424.295.91.891 1.463.592.553 1.104.826 1.527.826-2.123 0-.68.064 4.326.191 5.008.127 8.148.191 9.422.191-7.383 4.326-19.732 6.319-37.043 5.981-5.688-.084-10.566-2.588-14.639-7.51-3.992-4.669-5.984-9.888-5.984-15.658 0-6.108 2.057-11.308 6.176-15.595 4.113-4.282 9.229-6.427 15.338-6.427 1.357 0 3.16.297 5.41.891 2.248.594 3.756.891 4.518.891 3.139 0 7.045-1.293 11.713-3.883 4.666-2.588 6.875-3.883 6.621-3.883-.85 8.912-3.82 14.896-8.914 17.948-3.648 2.123-5.473 4.201-5.473 6.236 0 1.273.764 2.293 2.291 3.057 1.188.595 2.502.892 3.945.892 2.207 0 4.371-1.356 6.494-4.071 2.119-2.718 3.055-5.177 2.801-7.386-.254-2.545-.084-5.603.51-9.164.168-1.02.783-2.27 1.844-3.754 1.061-1.486 2.016-2.398 2.865-2.738 0 .762-.275 2.037-.828 3.818-.553 1.781-.826 3.1-.826 3.947 0 1.867.508 3.309 1.527 4.326 1.525-.592 2.883-2.502 4.074-5.729 1.016-2.459 1.609-4.836 1.781-7.127-3.566-.17-6.982-1.781-10.248-4.838-3.268-3.057-4.9-6.365-4.9-9.928 0-.594.082-1.188.256-1.783.508.764 1.271 1.953 2.289 3.564 1.443 2.121 2.547 3.182 3.313 3.182 1.016 0 1.525-1.061 1.525-3.182 0-2.715-.723-5.176-2.164-7.383-1.613-2.631-3.693-3.947-6.238-3.947-1.189 0-2.971.637-5.344 1.91-2.379 1.271-4.543 1.91-6.492 1.91-.596 0-3.229-.766-7.895-2.293 8.23-1.355 12.348-2.586 12.348-3.691 0-2.885-5.645-4.838-16.93-5.855-1.105-.084-3.141-.254-6.111-.51.338-.424 2.758-.891 7.258-1.4 3.818-.422 6.492-.637 8.018-.637 20.197 0 33.012 9.805 38.443 29.408.934-.773 1.402-2.066 1.402-3.871 0-2.324-.68-5.25-2.037-8.777-.512-1.375-1.318-3.441-2.42-6.193 6.957 8.867 10.439 17.27 10.439 25.199 0 4.178-.979 7.973-2.93 11.381-1.27 2.303-3.65 5.244-7.127 8.826-3.48 3.58-5.857 6.352-7.131 8.313 4.668-1.271 7.725-2.248 9.168-2.928 3.223-1.44 6.15-3.606 8.783-6.492 0 1.106-.467 2.762-1.4 4.967zm-55.502-50.025c0 1.525-.85 2.502-2.545 2.926l-3.311.51c-1.189.594-2.928 2.928-5.219 7-.256-1.271-.637-3.053-1.146-5.346-.764.086-2.035.764-3.818 2.037-.764.594-1.996 1.484-3.693 2.672.512-3.055 2.207-6.148 5.094-9.293 3.055-3.477 6.025-5.217 8.91-5.217 3.818 0 5.728 1.572 5.728 4.711zm22.15 11.709c0 1.443-.785 2.654-2.355 3.629-1.57.977-3.119 1.465-4.646 1.465-2.037 0-3.863-1.146-5.473-3.438-1.955-2.801-3.947-4.625-5.984-5.477.424-.422.934-.635 1.529-.635.764 0 2.055.594 3.881 1.781 1.824 1.189 2.99 1.783 3.502 1.783.424 0 1.123-.594 2.1-1.783.975-1.188 2.057-1.781 3.246-1.781 2.8.001 4.2 1.487 4.2 4.456z" fill="#0D0F0F"/></g></svg>

并编译为pdflatex -shell-escape sample.tex

它产生了这个(我喜欢):

在此处输入图片描述

但我被 困扰了[height=8px]。这是硬编码的,所以如果我改变字体大小,SVG 不会随之改变。如果我删除 的这一部分\includesvn{#1.svg},那么我最终会得到

在此处输入图片描述

我如何获取当前字体高度,以便可以相应地缩放 SVG?

答案1

\baselineskip将随字体大小缩放。使用此系数。

\documentclass{article}
\usepackage{svg}
\newcommand\mtg[1]{%
  \includesvg[height=0.6\baselineskip]{#1.svg}%
}

\begin{document}
\tiny         This magic card costs \mtg{R} mana\\
\scriptsize   This magic card costs \mtg{R} mana\\
\footnotesize This magic card costs \mtg{R} mana\\
\small        This magic card costs \mtg{R} mana\\
\normalsize   This magic card costs \mtg{R} mana\\
\large        This magic card costs \mtg{R} mana\\
\Large        This magic card costs \mtg{R} mana\\
\LARGE        This magic card costs \mtg{R} mana\\
\huge         This magic card costs \mtg{R} mana\\
\Huge         This magic card costs \mtg{R} mana\\
\end{document}

结果:

在此处输入图片描述

感谢 Phelype Oleinik 的回答。


优化(mrpiggi):

如果您经常使用此命令,那么从性能角度考虑,只包含图形本身一次并将其存储在一个框中以便重复使用是合理的:

\newcommand\mtg[1]{%
   \ifcsname mtg-#1-box\endcsname\else%
     \expandafter\newbox\csname mtg-#1-box\endcsname%
     \expandafter\sbox\csname mtg-#1-box\endcsname{%
       \includesvg[latex=false]{#1.svg}%
     }%
   \fi%
   \raisebox{-.05\baselineskip}{\resizebox{!}{.6\baselineskip}{%
     \expandafter\usebox\csname mtg-#1-box\endcsname%
   }}%
 }

相关内容