自定义模板创建

自定义模板创建

我有兴趣创建一个自定义模板,如下所示第 1 页

第 1 页布局

第2页 第 2 页布局

但目前,我正在使用以下代码,它没有生成所需的模板,任何帮助都将不胜感激。

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{graphicx}  % For including images
\usepackage{tabularx}
\usepackage{lipsum}    % For generating placeholder text. You can remove this in your final document.

% Page setup
\geometry{margin=1in}

% Define the page style
\fancypagestyle{firstpage}{ % Header for the first page
    \fancyhf{}
    \fancyhead[L]{\includegraphics[width=2cm]{example-image}}  % Left logo on the first page
    \fancyhead[R]{\includegraphics[width=2cm]{example-image}}  % Right logo on the first page
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}  % Remove horizontal line in header
}

\fancypagestyle{otherpages}{ % Header for subsequent pages
    \fancyhf{}
    \fancyhead[L]{\fancyplain{}{\leftmark}}  % Subject as header on other pages
    \fancyfoot[C]{\thepage}
    \renewcommand{\headrulewidth}{0pt}  % Remove horizontal line in header
}

\renewcommand{\maketitle}{\thispagestyle{firstpage} % Apply the first page header style
    \begin{titlepage}
        \vspace*{0cm}  % Remove the top space
        \centering
        \includegraphics[width=2cm]{example-image}  % Left top logo
        \hfill  % Right-align the following text
        \textbf{Internal Report}  % Centered text
        \hfill  % Right-align the following text
        \includegraphics[width=2cm]{example-image}  % Right top logo
        \vspace{1cm}
        \hrule  % Horizontal line
        \vspace{1cm}
        \begin{flushleft} % Begin left-aligned block
        \begin{tabularx}{\textwidth}{@{}X r@{}} % Left-aligned text and right-aligned text
            To: Name1 & Date: \today \\ % Add the date right-aligned next to "To"
            \vspace{1\baselineskip}  % One line spacing
            From: Name2 \\
            \vspace{1\baselineskip}  % One line spacing
            Cc: Name3 \\
            \vspace{1\baselineskip}  % One line spacing
            Subject: Title
        \end{tabularx}
        \end{flushleft} % End left-aligned block
        \vspace{1cm}
        \hrule  % Second horizontal line
    \end{titlepage}
}

\begin{document}

\maketitle

%\pagestyle{otherpages} % Apply the header style for subsequent pages

\section*{Summary}  % This is the "Summary" section
%\hrule  % Start the section right after the second horizontal line
Summary content goes here.

%\newpage % Start a new page

\section*{Introduction}  % This is the "Introduction" section
Your report content goes here.

\subsection*{Subsection}  % This is a subsection without a number
More content.

\section*{Conclusion}  % This is the "Conclusion" section
Concluding remarks.

\end{document}

当前代码的输出如下:第 1 页 在此处输入图片描述

和第 2 页 在此处输入图片描述

答案1

这是实现此目的的一种方法。基本思路:

  • 编写你自己的文档类tmplt.cls
  • 在主文档中使用它main.tex
  • 暂时忽略一些细微的调整障碍
  • 忽略所有字体问题,专注于 pdflatex

结果

解决你的主要问题

  • 分页符后\maketitle\tmplttitle{}改为使用(在 tmplt.cls 中)
  • 不过,肯定有一个解决方案来处理额外的分页符titlepage environment
  • 标题问题:已改编这是 2013 年的解决方案

为了激发您对排版的想法,我使用了两个小页面,而您的表格也可以正常工作。请注意(并亲自使用)%行末的所有内容\newcommand:这可以避免不必要的空格插入,从而避免不必要的布局偏移。

% ~~~ create your own title page ~~~~~~~~~~~~~
\newcommand\tmplttitle[0]{%
{{\parindent0pt% localized: disabling indenting of first line
    % ~~~ you could use your tabularx approach as well ~~~
...
    \bigskip%
    \hrule%
}}}

非常简单,一些花式字段的内容取决于页面(数字),使用\ifthenelse{cond}{if-case}{else-case},请参阅上面的链接。

\fancyhead[C]{\ifthenelse{\value{page}=1}%
                {\raisebox{.25in}{\large{Internal Report}}}%
                {\subj{}}}

处理 .cls 文件

在开发过程中,将其放在.cls与您的 相同的目录中是可以的main.tex

为了测试我的解决方案,只需将代码从最后复制到新目录中main.textmplt.cls

完成后,你可能想将课程移至TEXMF 根,并更新你的 Latex 发行版,例如通过你的MikTex 控制台之后,您就可以\documentclass{tmplt}像往常一样在任何地方使用它了。(并且不要忘记在那里提供文档。您迟早会自己需要它。)

tmplt.cls 提供的内容

默认名称:

% ~~~ default names, subject, date ~~~~~~~~~~~~~~
\newcommand\nameto[0]{Name 1}
...

默认图像,您可以根据您的标准进行更改:

% ~~~ default for images ~~~~~~~~~~~~~~~~~~~~~~~
\newcommand\imleft[0]{example-image}
\newcommand\imright[0]{example-image-duck}

为了方便起见,相关结构的快捷方式:

% ~~~ shortcuts to relevant sections ~~~~~~~~~~~~
\newcommand\concl[1]{\section*{Conclusion}#1}
\newcommand\intro[1]{\section*{Introduction}#1}
\newcommand\summ[1]{\section*{Summary}#1}

所述替换\maktitle和页面相关的标题处理。

在 main.tex 中使用 tmplt.cls

  • 指定此类
  • 使用覆盖默认值\renewcommand
  • 写出总结、简介、结论
  • 使用标准 Latex

这是一个基本方案,您可以保存它以供重复使用...

\documentclass{tmplt}
\renewcommand\mydate{11/04/2023}
\begin{document}
 \tmplttitle{}
 \summ{enter here}
 \intro{enter here}
...
 \concl{ener here}
\end{document}

因此,如果您有一个专用/img目录,您可以像这样覆盖:

\renewcommand\imleft{img/logo}

代码

tmplt.cls:

% see e.g. "The Latex Companion", Mittelbach et. al.

% ~~~ 1) identification ~~~~~~~~~~
  \NeedsTeXFormat{LaTeX2e}
  \ProvidesClass{tmplt}[2023/11/11]

% ~~~ 2) initial code ~~~~~~~~~~
%        instead of \usepackage call
  \RequirePackage{ifthen}
  \RequirePackage{fancyhdr}
  \RequirePackage{graphicx}


% ~~~ 3) declaration of options ~~~~~~~~~~

% ~~~ 4) execution of options ~~~~~~~~~~

% ~~~ 5) package loading ~~~~~~~~~~
  \LoadClass[12pt, letterpaper]{article}%       use as base class
  \RequirePackage[%showframe,% uncomment showframe to check layout settings
    margin=1in,top=1.8in,headheight=1.5in]{geometry}


% ~~~ 6)  main code ~~~~~~~~~~
% ~~~ these ones can be useful for horizontal and vertical alignment ~~
\newcommand\hs[1]{\hspace{\stretch{#1}}}
%\newcommand\vs[1]{\vspace{\stretch{#1}}}

% ~~~ default names, subject, date ~~~~~~~~~~~~~~
\newcommand\nameto[0]{Name 1}
\newcommand\namefrom[0]{Name 2}
\newcommand\namecc[0]{none}
\newcommand\subj[0]{This is the title}
\newcommand\mydate[0]{\today{}}

% ~~~ default for images ~~~~~~~~~~~~~~~~~~~~~~~
\newcommand\imleft[0]{example-image}
\newcommand\imright[0]{example-image-duck}

% ~~~ shortcuts to relevant sections ~~~~~~~~~~~~
\newcommand\concl[1]{\section*{Conclusion}#1}
\newcommand\intro[1]{\section*{Introduction}#1}
\newcommand\summ[1]{\section*{Summary}#1}

% ~~~ create your own title page ~~~~~~~~~~~~~
\newcommand\tmplttitle[0]{%
{{\parindent0pt% localized: disabling indenting of first line
    % ~~~ you could use your tabularx approach as well ~~~
%   \hrule%
    \bigskip%
  % ~~~ typesetting left column ~~~~~
  \begin{minipage}{8cm}%
    \begin{tabular}{ll}%
        To:     & \nameto{}\\%
        From:   & \namefrom{}\\%
        Cc:     & \namecc{}\\%
        Subject:& \subj{}\\%
    \end{tabular}%
  \end{minipage}%   
  \hs{1}%
  % ~~~ typesetting right entry ~~~~~~~~
  \begin{minipage}{3cm}%
    \mydate{}%
  \end{minipage}%
    \bigskip%
    \hrule%
}}}

% ~~~ taking care of headers and footers ~~~~~~~~~~
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\includegraphics[width=2cm]{\imleft}}

% ~~~ adapted from https://tex.stackexchange.com/a/122379/245790 ~~~
\fancyhead[C]{\ifthenelse{\value{page}=1}%
                {\raisebox{.25in}{\large{Internal Report}}}%
                {\subj{}}}
\fancyhead[R]{\ifthenelse{\value{page}=1}{%
        \includegraphics[width=2cm]{\imright}%
    }%
    {\thepage}}

主要.tex:

\documentclass{tmplt}%  <<< work with your template
\usepackage{lipsum}

% ~~~ overwrite tmplt variables as needed ~~~~~
\renewcommand\nameto{Dr. Cooper, Dr. Hofstadter}
\renewcommand\subj{This is the title, in a modified version}
\renewcommand\mydate{11/04/2023}

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}

 % ~~~ 1) show the title pages as defined in tmplt ~~~
 \tmplttitle{}

 % ~~~ 2) put your summary, from tmplt ~~~
 \summ{Something short.\\%
    Or perhaps not so short. Or even a bit more. The sky's the limit. Whatever limits the sky.%
 }
 
 % ~~~ 3) put your introduction, from tmplt ~~~
 \intro{\lipsum[10]}
 
 % ~~~ 4) use standards here ~~~
     \section*{Where it all started}
     Let's reflect the starting point a little.
     
     \subsection*{Where we were}
     \lipsum[12]
     
     \subsection*{Where we wanted to go}
     \lipsum[13]
     
     \section*{And so the story went}
     \lipsum[1-5]
 
 % ~~~ 5) put your conlcusion here, from tmplt ~~~
 \concl{Whatever it took.}
 
\end{document}

相关内容