newcommand 다중 기본값

newcommand 다중 기본값

나는 자주 변경되는 문서로 바쁘다. 특수하게 사용된 치수가 변경됩니다. 이로 인해 치수를 변경할 때 값을 놓칠 가능성이 커집니다. 이 문제를 해결하기 위해 저는 3개의 변수(L,W,H)를 사용하여 명령을 만들었습니다. 의 도움으로http://en.wikibooks.org/wiki/LaTeX/Macros나는 다음 명령을 내린다.

\newcommand{\dimsEN}[3][default value]{The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{#1} \times \mathrm{#2} \times \mathrm{#3}$}

이제 3개의 기본값을 만드는 방법을 알 수 없습니다. 모든 값을 변경할 수 있는 한 곳이 있습니다.

감사해요

답변1

키-값 인터페이스가 필요하므로 값을 설정하는 순서는 관련이 없습니다.

\documentclass{article}
\usepackage{keyval}

\makeatletter
\define@key{janbertdims}{l}{\def\janbert@l{#1}}
\define@key{janbertdims}{w}{\def\janbert@w{#1}}
\define@key{janbertdims}{h}{\def\janbert@h{#1}}
% initialize; change here to your preferred values
\setkeys{janbertdims}{
  l=1,        
  w=1,    
  h=1,
}

\newcommand\dimsEN[1][]{%
  \begingroup
  \setkeys{janbertdims}{#1}% the current values
  The dimensions ($\mathrm{L} \times \mathrm{W} \times \mathrm{H}$)
  of the room are
  $\janbert@l \times \janbert@w \times \janbert@h$%
  \endgroup
}
\makeatother

\begin{document}

\dimsEN

\dimsEN[h=3]

\dimsEN[h=4,w=2]

\dimsEN[w=3,l=5,h=2]

\end{document}

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

동일하지만 expl3구문이 있습니다.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\dimsEN}{ O{} }
 {
  \group_begin: % keep the changes to the keys local
  \janbert_dims_en:n { #1 }
  \group_end:
 }

\keys_define:nn { janbert/dims }
 {
  l .tl_set:N  = \l__janbert_dims_l_tl,
  w .tl_set:N  = \l__janbert_dims_w_tl,
  h .tl_set:N  = \l__janbert_dims_h_tl,
  %%% default values 
  l .initial:n = { 1 },
  w .initial:n = { 1 },
  h .initial:n = { 1 },
 }

\cs_new_protected:Npn \janbert_dims_en:n #1
 {
  \keys_set:nn { janbert/dims } { #1 }

  The ~ dimensions ~ ($\mathrm{L} \times \mathrm{W} \times \mathrm{H}$)
  of ~ the ~ room ~ are ~ 
  $\l__janbert_dims_l_tl \times \l__janbert_dims_w_tl \times \l__janbert_dims_h_tl$
}
\ExplSyntaxOff

\begin{document}

\dimsEN

\dimsEN[h=3]

\dimsEN[h=4,w=2]

\dimsEN[l=5,h=2,w=3]

\end{document}

답변2

세 가지 모두 다음을 사용하여 설정할 수 있습니다 \dimsEN<value>.

\documentclass[12pt]{article}
\makeatletter
\def\dimsEN{\@ifnextchar[\dimsEN@i\dimsEN@ii}
\def\dimsEN@i[#1]#2#3{%
  The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{#1} \times \mathrm{#2} \times \mathrm{#3}$}
\def\dimsEN@ii{\@ifnextchar<\dimsEN@iii{\dimsEN@i[3]}}%%      3 is the default
\def\dimsEN@iii<#1>{\dimsEN@i[#1]{#1}{#1}}
\makeatother
\begin{document}

\dimsEN{1}{2}% default #1=3 

\dimsEN<5>% all are 5

\dimsEN[1]{2}{3}

\end{document}

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

또 다른 가능성은 쉼표로 구분된 목록을 사용하는 것입니다.

\documentclass[12pt]{article}
\makeatletter
\def\dimsEN#1{\dimsEN@i#1\@nil}
\def\dimsEN@i#1,#2,#3\@nil{%
  \ifx\relax#1\relax \def\@argA{1}\else\def\@argA{#1}\fi%   the default is always 1
  \ifx\relax#2\relax \def\@argB{1}\else\def\@argB{#2}\fi%  
  \ifx\relax#3\relax \def\@argC{1}\else\def\@argC{#3}\fi% 
  The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{\@argA} \times \mathrm{\@argB} \times \mathrm{\@argC}$}
  \makeatother
\begin{document}

\dimsEN{1,2,3} \par
\dimsEN{1,2,}  \par
\dimsEN{1,,}   \par
\dimsEN{,2,3}  \par
\dimsEN{,,3}   \par
\dimsEN{,,}  

\end{document}

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

쉼표가 있는지 확인하는 것도 가능하지만 순서가 중요합니다.

관련 정보