내 패키지의 key-val 옵션을 설정하는 데 도움이 필요합니다.

내 패키지의 key-val 옵션을 설정하는 데 도움이 필요합니다.

내 패키지에 키-값 옵션을 사용하고 싶고 pgfopts로 솔루션을 시도했습니다. 1) 예를 들어 패키지를 로드하는 것이 가능해야 합니다.

\usepackage[colors=1]{mypack}

2) 프리앰블뿐만 아니라 사용자 코드 어딘가에서 색상을 변경하려면 다음과 같이 하세요.

\MPset{colors=2}

3) 어떤 색상이 활성화되어 있는지 테스트합니다.

내 실제 패키지에는 언어 및 메시지와 관련된 2가지 옵션이 더 있으므로 pgfopts가 과잉일 수도 있습니다. 나는 key-val 패키지를 사용하는 모든 작업 솔루션을 기꺼이 받아들입니다.

\ProvidesPackage{mypack}
\RequirePackage{%
xcolor,
pgfopts
}

\def\MPset#1{\pgfqkeys{/MP}{ /#1}}
\MPset{colors/.initial=2}
\MPset{colors/.is choice}
\MPset{colors/1/.code={\def\test{\textcolor{black}{TEST}}}}
\MPset{colors/2/.code={\def\test{\textcolor{red}{TEST}}}}
\MPset{colors/3/.code={\def\test{\textcolor{blue}{TEST}}}}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty

이 테스트 문서에서는 mypack을 사용합니다.

\documentclass{article}
%\usepackage[colors=1]{mypack}
\usepackage{mypack}
\MPset{colors=2}
\begin{document}
  :\pgfkeysvalueof{colors}:
  \test
\end{document}
\endinput

mypack을 로드하면

\usepackage[colors=1]{mypack}

이 오류가 발생합니다.

! Package pgfkeys Error: I do not know the key '/MP/colors', to which you
  passed '1', and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.16 \ProcessPgfPackageOptions{/MP}

옵션 없이 mypack을 로드하면 컴파일되지만

\pgfkeysvalueof{colors}

비었다. 주석을 해제할 때

\test

원하는 색상의 텍스트를 얻었지만 여전히 어떤 색상이 활성화되어 있는지 테스트할 수 없습니다.

답변1

수정된 코드 버전은 다음과 같습니다.

\documentclass{article}
%------------------------------
\usepackage{filecontents}
\begin{filecontents*}{mypack.sty}
\ProvidesPackage{mypack}
\RequirePackage{%
xcolor,
pgfopts
}

\pgfkeys{/MP/.is family}
\def\MPset#1{\pgfkeys{/MP,#1}}
\MPset{
  colors/.is choice,
  colors/1/.code={\def\test{\textcolor{black}{TEST}}},
  colors/2/.code={\def\test{\textcolor{red}{TEST}}},
  colors/3/.code={\def\test{\textcolor{blue}{TEST}}},
  % default value
  colors=2,
}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty
\end{filecontents*}
%------------------------------

\usepackage[colors=3]{mypack}
%\MPset{colors=2}
\begin{document}
  \test
\end{document}

다음은 (하려고 노력하다) 귀하의 요구 사항과 일치하십시오.

  • 매크로 는 TeX-if를 \MPspecial사용하여 MP@uses@italic기울임체 또는 색상 중에서 선택합니다.
  • 키 는 TeX-if를 true 또는 false로 use italic설정합니다 .MP@uses@italic
  • define current color는 xcolor를 통해 현재 색상을 정의하고 현재 이름을 \MP@current@color.
  • 열쇠 colors는 세 가지 대안(검은색, 파란색, 빨간색)이 있는 선택이며 를 정의합니다 MP current color. 빨간색과 파란색은 use italicfalse로 설정되고 검정색은 use italictrue로 설정됩니다.
\documentclass{article}
%------------------------------
\usepackage{filecontents}
\begin{filecontents*}{mypack.sty}
\ProvidesPackage{mypack}
\RequirePackage{xcolor}
\RequirePackage{pgfopts}

\newif\ifMP@uses@italic
\pgfkeys{/MP/.is family}
\def\MPset#1{\pgfkeys{/MP,#1}}
\MPset{
  % some special macros use italic istead of black
  use italic/.is if=MP@uses@italic,
  use italic=false, % default value
  % define the current color
  define current color/.code={\def\MPcurrent@color@name{#1}\colorlet{current MP color}{#1}},
  % 
  colors/.is choice,
  colors/black/.style={use italic=true,define current color=black},
  colors/red/.style={use italic=false,define current color=red},
  colors/blue/.style={use italic=false,define current color=blue},
  % default value
  colors=red,
}

\def\MPspecial#1{\ifMP@uses@italic\textit{#1}\else\textcolor{current MP color}{#1}\fi}
\def\test{\textcolor{current MP color}{TEST (in \MPcurrent@color@name)}}

\ProcessPgfPackageOptions{/MP}
\endinput% mypack.sty
\end{filecontents*}
%------------------------------
\pagestyle{empty}
\usepackage[colors=red]{mypack}
\begin{document}
\test{} \MPspecial{My text}

\MPset{colors=black}
\test{} \MPspecial{My text}

\MPset{colors=blue}
\test{} \MPspecial{My text}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보