재사용 가능하고 쉽게 편집할 수 있는 키워드 정의

재사용 가능하고 쉽게 편집할 수 있는 키워드 정의

나는 아직 문체를 모두 선택하지 않은 채 논문을 쓰고 있습니다. 즉, 논문의 저자가 "나", "우리", "저자" 또는 "저자" 등인 경우 어떤 긴 단어를 축약하고 어떻게 축약하고 싶은지 결정하지 않았습니다. . 그래서 제가 하고 싶은 것은 여러 번 재사용될 몇 가지 키워드를 정의하는 것입니다.

내 문서 상단에서 다음과 같은 작업을 수행하고 싶고 문서에 \newcommand{\auth}{we}쓸 때마다 "우리"라는 단어가 표시되어야 합니다. \auth{}이제, 좀 더 노련한 여러분은 내가 여기서 설명하는 내용이 실제로 내가 원하는 대로 실제 질문에 적용된다는 것을 알게 될 것입니다.

내가 정말로 원하는 것은 영어 사전에 있는 단어를 명령에 연결하고 해당 명령에 인수를 전달할 때 출력을 약간 변경할 수 있는 것입니다. 예를 들어 내가 \auth{}"우리"를 내뱉기로 정의했다면 \auth{upper}대신 "우리"를 내뱉고 싶습니다. 또한 \auth{upper, possessive}이나 \auth{possessive}이와 유사한 호출을 통해 "Ours" 및 "ours"와 같은 소유격 형태를 얻을 수 있기를 원합니다 . 이를 달성하는 가장 좋은 방법은 무엇입니까? 일종의 사전을 만들 수 있나요? 아니면 무엇을 추천하시나요?

정말 감사합니다!

답변1

글쎄, 나는 노력했다. 그런데 LuaLaTeX에서만 작동합니다.

%!TEX program = lualatex
\documentclass{article}
\usepackage{polyglossia}
\usepackage{luacode}
\begin{luacode*}
dofile(kpse.find_file("l-lpeg.lua"))
dofile(kpse.find_file("util-sto.lua"))
dofile(kpse.find_file("util-prs.lua"))
Pronouns = {
    ["formal"] = {
        ["possessive"] = "ours",
        ["nominative"] = "we",
        ["oblique"] = "us"
    },
    ["informal"] = {
        ["possessive"] = "mine",
        ["nominative"] = "I",
        ["oblique"] = "me"
    }
}
function Auth(keywords)
    local dummy = utilities.parsers.settings_to_array(keywords)
    for i,v in ipairs(dummy) do
        if Pronouns[dummy[i]] ~= nil then result = Pronouns[dummy[i]] end
    end
    for i,v in ipairs(dummy) do
        if result[dummy[i]] ~= nil then result = result[dummy[i]] end
    end
    return result
end
function Upper(string)
    return unicode.utf8.upper(unicode.utf8.sub(string,1,1))..unicode.utf8.sub(string,2,utf8.len(string))
end
\end{luacode*}
\def\auth#1{\directlua{tex.print(Auth("#1"))}}
\def\Auth#1{\directlua{tex.print(Upper(Auth("#1")))}}
\begin{document}
\auth{oblique,informal} \Auth{formal,possessive}
\end{document}

답변2

이것은 놀라울 정도로 복잡했지만 여기에는 모든 TeX*에서 작동하는 솔루션이 있습니다.

\begingroup
\catcode`!=11 % for private macros
\endlinechar=-1
\catcode`\^^M=12 \catcode`\^^?=12\relax
\gdef\!stop{^^?}
\gdef\!fi{\fi}
\newtoks\!before \newtoks\!after

% some convenient shorthands
\gdef\!csnm#1{\csname#1\endcsname} 
\gdef\!ecsnm#1#2{\expandafter#1\csname#2\endcsname}

% the main command sets up the catcodes for reading in
% the definitions of the second argument
\gdef\newvarcommand#1{
  \begingroup\catcode`\^^M=12 \catcode`\^^?=12\relax
  {\escapechar=-1 \xdef\!name{\string#1}}
  \!ecsnm\newtoks{toks:\!name}
  \gdef#1##1{
    {\escapechar=-1 \xdef\!name{\string#1}}
    \begingroup
    \!processnextkey##1,^^?,
    \!ecsnm\the{toks:\!name}
    \!result
    \endgroup
  }
  \!newvarcommand
}

% for each modifier in the argument, set the corresponding
% conditional true
\gdef\!processnextkey#1,{\def\arg{#1}
  \ifx\!stop\arg\else
    \ifx\empty\arg\def\!key{default}\else\def\!key{#1}\fi
    \!ecsnm\ifx{ifkey:\!name:\!key}\relax
      \errmessage{Unknown key \!key\space for command \!name}
    \else\!csnm{key:\!name:\!key true}\fi
  \expandafter\!processnextkey\fi
}

% here we read the argument line by line
\gdef\!newvarcommand#1{\!getnext#1^^M^^?^^M}
\gdef\!getnext#1^^M{\def\arg{#1} 
  \ifx\!stop\arg\endgroup\else
  \ifx\empty\arg\else\!parse#1^^M\fi
  \expandafter\!getnext\fi
}

% for each entry, new conditionals are created to test whether a
% modifier is present or not, and a token list containing the
% conditionals and the word to be printed is appended to the
% token list read by the command to be defined
\gdef\!parse#1:#2^^M{\!before={}\!after={}\def\arg{#1}
  \ifx\empty\arg
    {\globaldefs=1 \!ecsnm\newif{ifkey:\!name:default}}
    \!before=\expandafter{\csname ifkey:\!name:default\endcsname}
    \!after=\expandafter{\!fi}
  \else\!setupnextkey#1,^^?,\fi
  \edef\!addtoks{\!ecsnm\the{toks:\!name}
    \the\!before\def\noexpand\!result{#2}\the\!after}
  \global\!csnm{toks:\!name}=\expandafter{\!addtoks}
}

% creating \newifs for each modifier
\gdef\!setupnextkey#1,{\def\arg{#1}
  \ifx\!stop\arg\else
    {\globaldefs=1 \!ecsnm\newif{ifkey:\!name:#1}}
    \!before=\expandafter{\the\expandafter\expandafter
      \expandafter\!before\!csnm{ifkey:\!name:#1}}
    \!after=\expandafter{\the\expandafter\!after\!fi}
  \expandafter\!setupnextkey\fi
}
\endgroup

그런 다음 "사전"을 다음과 같이 정의할 수 있습니다.

\newvarcommand\auth{
  :we
  upper:We
  possessive:ours
  upper,possessive:Ours
}

각 줄에 인수로 사용하려는 단어를 쉼표로 구분하여 작성한 다음 콜론 뒤에 표시할 단어를 입력합니다. 기본값은 또는 로 지정할 수 :val있습니다 default:val.중요한:항목은 지정자 수가 증가하는 순서로 발생해야 합니다(즉, 모든 한 단어 항목이 먼저 있어야 하고 그 다음 쉼표로 구분된 두 단어가 있는 항목, 그 다음 세 개 등이 있어야 합니다). 그렇지 않으면 잘못된 결과가 나타날 수 있습니다.

\auth{} \auth{default} % these two are the same, "we"
\auth{upper} % "We"
\auth{possessive} % "ours"
\auth{upper,possessive} \auth{possessive,upper} % both "Ours"
\auth{lower} % this gives an "Unknown key" error

upper,possessive:Ours코드가 어떻게 작동하는지 설명하겠습니다 . 기본적으로 해당 행을 읽으면 먼저 조건문 \ifkey:auth:upper\ifkey:auth:possessive. 그런 다음 토큰 목록

\ifkey:auth:upper
  \ifkey:auth:possessive
    \def\!result{Ours}
  \fi
\fi

라는 토큰 목록이 구성되어 추가됩니다 \toks:auth. 를 사용하면 \auth{upper,possessive}인수를 읽고 해당 조건을 \ifkey:auth:uppertrue ifkey:auth:possessive로 설정하면 toks:auth토큰 목록이 압축 해제되어 \!result인쇄됩니다.


\newif* LaTeX처럼 외부가 아닌 것으로 가정 하지만 일반 TeX의 경우 일부 조정이 필요할 수 있습니다.

관련 정보