템플릿에 사용할 사용자 정의 콘텐츠를 로드하는 방법은 무엇입니까?

템플릿에 사용할 사용자 정의 콘텐츠를 로드하는 방법은 무엇입니까?

report저는 클래스를 사용하고 여러 패키지로 조판을 사용자 정의하는 템플릿을 작성 중입니다 . user-data.tex바로 다음에 로드되는 별도의 파일에 일부 데이터를 정의하고 있습니다 documentclass.

\def\thesis{Master} %<PhD> or <Master>
\def\thesistitle{The title of the thesis}
\def\author{AName AFirst ASecond}
\def\authormail{[email protected]}
\def\school{Master and Doctoral School}
\def\date{City, month year}
\def\logo-university{univ.pdf}

이러한 변수/상수는 두 개의 서로 다른 제목 페이지를 제공하는 데 사용되며, 바닥글에 포함되거나 titlesec사용자가 언제든지 사용할 수 있습니다.

그렇게 해도 괜찮습니까(단지 사용 \def)? 아니면 \global, \newcommand... 또는 다른 것을 사용해야 합니까 ? 일반 형식이 아닌 다른 형식으로 배치하시겠습니까 tex?


\newcommand*댓글을 작성한 후 일부 매크로를 변경 하고 이름을 바꿨습니다.

\newcommand*\thesis{Master} %<PhD> or <Master>
\newcommand*\thesistitle{The title of the thesis}
\newcommand*\authors{AName AFirst ASecond}
\newcommand*\authorsmail{[email protected]}
\newcommand*\school{Master and Doctoral School}
\newcommand*\titledate{City, month year}
\newcommand*\university{univ.pdf}

@Andrew의 답변 후에 나는 다음을 사용하고 있습니다.

\providecommand*\@school{No SCHOOL defined}
\newcommand*\School[1]{\renewcommand*\@school{#1}}

패키지 시작 부분에 사용자가 \newcommand*\@school{Something}로드하기 전이나 로드 \School{Something}한 후에 정의할 수 있도록 합니다.


@barbara가 추측한 대로 템플릿을 *.sty패키지로 로드할 위치로 옮겼습니다.

mypkg.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypkg}

%If not previously defined, default value applied
\providecommand*{\@mythesis}{No THESIS TYPE defined}
\providecommand*{\@myauthor}{No AUTHOR defined}
%Command to modify the field at any point after this
\newcommand*\MyThesis[1]{\renewcommand*\@mythesis{#1}}
\newcommand*\MyAuthor[1]{\renewcommand*\@myauthor{#1}}
%Command to access the field
\newcommand*\showDF[1]{\csname @#1\endcsname}

%The content may be supplied in a separate file with \Thesis
\@input{data.dat}

\endinput

main.tex

\documentclass[a4paper,titlepage,11pt,twoside,openright]{report}

% A field may defined before loading the package
\makeatletter
\newcommand\@mythesis{My title before loading}
\makeatother
%If it not defined, the package does it
\usepackage{mypkg}

\begin{document}

% The variable may be directly accessed
\makeatletter\@mythesis\makeatother
% The content may be rewritten
\MyThesis{PhD}
% Reading with the provided command
\showDF{mythesis}
\showDF{myauthor}

\end{document}

효과가있다. 사용자는 패키지를 로드하기 전, 별도의 파일에서 또는 '도우미 명령'을 사용하여 수행한 후 언제든지 콘텐츠를 정의할 수 있습니다. 그러나 나는 다음을 원합니다:

- 명령을 사용하여 필드와 관련된 명령을 자동으로 정의합니다. 새로운 질문여기.

-사용자가 패키지를 로드할 때 해당 필드를 옵션으로 제공할 수 있는 기능을 제공합니다. 나는 다음을 시도했다 xkeyval:

\define@key{fam}{thesis}[Master]{\Thesis{#1}}
\ProcessOptionsX<fam>

기본 파일의 로딩을 다음과 같이 변경합니다.

\usepackage[thesis=PhD]{mypkg}

위의 코드는 단어 사이의 공백이 제거되므로 단어가 두 개 이상인 필드를 제외하고는 작동합니다. 어떻게 작동하게 할 수 있습니까 \usepackage[title=A title with several words]{mypkg}? 나는 시도했지만 {A title with several words}작동 "A title with several words"하지 않습니다.

만들 수 없습니다


접근 방식을 사용하여이것답변, 이제 ​​모든 명령을 한 번에 생성하는 '공장' 기능이 생겼습니다.

\usepackage{etoolbox}
\newcommand\forcsvlistargs[2]{ \expandafter\providecommand\csname \detokenize{#1}aux\endcsname[1]{
 \csname \detokenize{#1}\endcsname##1\relax}
 \forcsvlist{\csname \detokenize{#1}aux\endcsname}{#2}
}
\newcommand\newDF[2]{ \expandafter\providecommand\csname th#1\endcsname{No \MakeUppercase{#2} defined}
 \expandafter\newcommand\csname TH#2\endcsname[1]{\expandafter\renewcommand\csname th#1\endcsname{##1}} }

\forcsvlistargs{newDF}{{{typ}{type}}, {{date}{date}}, {{tit}{title}}, {{sch}{school}}, 
{{aut}{author}}, {{eaut}{eauthor}} }

답변1

이와 같은 작업을 수행하는 (내부) 클래스가 있습니다. 내가 하는 일은 먼저 다음과 같은 명령을 정의하는 것입니다.

\newcommand\School[1]{\def\@school{#1}}

\School{My wonderful school}"사용자"가 기본값을 덮어 쓸 수 있도록 합니다 . 내부적으로 클래스는 \@school학교 이름을 인쇄해야 할 때마다 를 사용합니다.

다음으로, 이것이 합리적이라면 다음과 같은 줄을 사용하여 수업에서 학교에 대한 합리적인 기본값을 설정하도록 합니다.

\School{My Wonderful School}

수업 중. 실제로 사용하는 것이 더 나은 형식일 수 있습니다.

\providescommand\@school{My Wonderful School}

"도우미 명령"을 사용하는 것이 더 명확하다고 생각합니다.

내가 이것을 사용하는 한 곳은 이러한 변수 중 많은 부분이 다른 것에 의존하는 튜토리얼 시트입니다. 이러한 경우에는 이러한 모든 변수를 한 번에 설정하기 위해 클래스에 옵션을 정의합니다. 따라서 "사용자"는 다음으로 파일을 시작합니다.

\documentclass[somecourse,solutions]{mytutorials}

그리고 그 mytutorials.cls안에는

\DeclareOption{somecourse}{
  \CourseName{An exciting counrse}
  \Semester{Semester 2}
  \CourseNumber{Math 987123}
}
\DeclareOption{solutions}{
    ...
}

모든 옵션을 처리하려면 다음과 같은 것이 필요합니다.

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions

article.cls이를 통해 내 파일에 정의되지 않은 옵션을 피기백하여 전달할 수 있습니다 .

마지막으로 사용 사례에 따라 현재 디렉터리의 파일에서 기본값 집합을 자동으로 로드할 수 있습니다. \include또는를 사용하면 \input파일이 존재하지 않을 때 문제가 발생합니다. 대신 \@input클래스 파일 내부에서 사용할 수 있습니다 .

\@input{defaults.dat}

"defaults.dat" 파일이 있으면 로드하고, 그렇지 않으면 아무 작업도 수행하지 않습니다. 이 파일은 있는 그대로 포함되므로 이 트릭을 사용하면 파일에 쓰레기가 포함되어 있으면 모든 것이 손상될 위험이 있습니다. 물론 이 파일이 존재하지 않는 경우를 처리하려면 합리적인 기본값을 설정해야 합니다.

(그런데, 여러분 자신의 내부 변수에 대해서는 실제로 사용해도 아무런 해가 없으며 \def더 쉽습니다. 물론, 먼저 다른 것을 덮어쓰지 않는지 확인해야 합니다!)

관련 정보