
파일 이름을 입력으로 사용하여 확장자를 변경하고 이를 \includegraphics
.
나는 보았다\includegraphics, 다른 확장 프로그램을 사용해 보세요, 대체 솔루션을 제공하지만 왜 내가endcs이름그리고매개변수 번호현재 코드에 오류가 있습니다(또한 해당 솔루션은 xelatex에서 작동하지 않는 것 같습니다).
\documentclass{article}
\usepackage{graphicx}
\usepackage{xstring}
\usepackage{letltxmacro}
\LetLtxMacro{\IncludeGraphics}{\includegraphics}
\newcommand{\svgtopdf}[1]
{\IfSubStr{#1}{.svg}{\StrSubstitute*{#1}{.svg}}{#1}}
\newcommand{\includegraphicsNaive}[2][]
{\IncludeGraphics[#1]{\svgtopdf{#2}}}
\newcommand{\includegraphicsEdef}[2][]
{\edef\pdfname{\svgtopdf{#2}}
\IncludeGraphics[#1]{\pdfname}}
\newcommand{\includegraphicsNoExpand}[2][]
{\edef\x{\noexpand\IncludeGraphics[#1]{\svgtopdf{#2}}}%
\x{}}
\newcommand{\includegraphicsExpandafter}[2][]
{\IncludeGraphics[#1]\expandafter{\svgtopdf{#2}}}
\begin{document}
\includegraphicsNaive{img.svg} % ERROR: Missing endcsname inserted.
\includegraphicsEdef{img.svg} % ERROR: Illegal parameter number in definition of \pdfname.
\includegraphicsNoExpand{img.svg} % ERROR: Illegal parameter number in definition of \x.
\includegraphicsExpandafter{img.svg} % ERROR: LaTeX Error: File `' not found.
\end{document}
첫 번째 접근 방식은 순진한 접근 방식이었습니다.생각하다작동하지 않는 이유는 내부적으로 graphicx가 각 파일 형식에 대해 다른 매크로를 사용하기 위해 파일 이름 인수(파일 확장자)의 일부를 포함하는 명령을 구성하고 인수를 확장하지 않는 것 같습니다. 그 일을 하기 전에.
두 번째 접근 방식은 같은 이유로 실패할 수 있지만(
\pdfname
확장되지 않음) 왜 다른 오류가 발생하는지 잘 모르겠습니다.세 번째는 기대하고 있었는데 왜 작동하지 않는지 모르겠습니다.
내가 생각하는 마지막 것은 그것이 마법을 발휘하기 전에
expandafter
소비 되었을 것입니다.includegraphics
올바른 방법은 무엇입니까?
답변1
매크로 xstring
에는 작업 결과를 매크로에 저장하는 데 사용되는 선택적 인수가 끝에 있으며, 이 경우에 사용할 수 있습니다.
또한 \StrSubstitute
별표 버전이 없습니다. 또한 전체 문자열, 검색 문자열 및 대체 문자열의 세 가지 인수를 제공해야 하지만 MWE에는 전체 문자열과 검색 문자열만 있습니다. 원하는 경우 대체 문자열을 비워( \StrSubstitute{#1}{.svg}{}[\tmpname]
) 사용 가능한 모든 확장을 시도할 수 있습니다.
MWE 작동 중:
\documentclass{article}
\usepackage{graphicx}
\usepackage{xstring}
\usepackage{letltxmacro}
\LetLtxMacro{\IncludeGraphics}{\includegraphics}
\newcommand{\svgtopdf}[1]
{\IfSubStr{#1}{.svg}{\StrSubstitute{#1}{.svg}{.pdf}[\tmpname]}{\def\tmpname{#1}}}
\newcommand{\includegraphicsNaive}[2][]
{\svgtopdf{#2}%
\IncludeGraphics[#1]{\tmpname}}
\begin{document}
\includegraphicsNaive[width=3cm]{example-image.svg}
\includegraphicsNaive[width=3cm]{example-image-a.pdf}
\includegraphicsNaive[width=3cm]{example-image-b}
\end{document}
결과: