第一部分

第一部分

我是 LaTeX 新手。我正在尝试调整一个基本模板(来自http://www.latextemplates.com/template/programming-coding-assignment),这样我就可以附加一个默认环境标签。例如,我想采用以下环境:

\newcommand{\homeworkProblemName}{}
\newenvironment{homeworkProblem}[1][Part \Alph{homeworkProblemCounter}]{...}

并进行调整,以便我可以将一些文本行附加到默认标签中,这些文本行在后续的 homeworkProblem 环境中可能是唯一的(或省略)。

\begin{homeworkProblem}
...
\end{homeworkProblem}

结果是:

第一部分

但我想在该标签上附加自定义描述,例如:

A 部分 - 第一部分

在后续的家庭作业问题环境中,我希望默认标签能够正常进行(第 B 部分、C 部分,...),但允许每个标签附加一个唯一的短语(第二部分、第三部分,...)。

我意识到我可以使用 [1] 参数并简单地传递“[第 A 部分 - 第一部分]”,但我希望“第 A 部分”、“B”、“C”(等等)部分自然迭代,而无需我将 [第 A 部分 ...] 作为参数的一部分传递。

这应该很简单,但我一直很难找到解决方案。感谢您的帮助。

答案1

您需要判断是否提供了可选参数homeworkProblem(参见如何检查宏值是否为空或不会使用纯 TeX 条件创建文本?)。以下定义就足够了:

\newenvironment{homeworkProblem}[1][]{%
  \stepcounter{homeworkProblemCounter}%
  \if\relax\detokenize{#1}\relax
    \renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter}}%
  \else
    \renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter} - #1}%
  \fi
  \section{\homeworkProblemName}
  \enterProblemHeader{\homeworkProblemName}%
}{%
  \exitProblemHeader{\homeworkProblemName}%
}

作为参考,下面是使用模板示例的完整代码

\begin{homeworkProblem}[first]
% ...
\end{homeworkProblem}

对于第一个问题

\begin{homeworkProblem}
% ...
\end{homeworkProblem}

第二个:

在此处输入图片描述

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Programming/Coding Assignment
% LaTeX Template
%
% This template has been downloaded from:
% http://www.latextemplates.com
%
% Original author:
% Ted Pavlic (http://www.tedpavlic.com)
%
% Note:
% The \lipsum[#] commands throughout this template generate dummy text
% to fill the template out. These commands should all be removed when 
% writing assignment content.
%
% This template uses a Perl script as an example snippet of code, most other
% languages are also usable. Configure them in the "CODE INCLUSION 
% CONFIGURATION" section.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%   PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass{article}

\usepackage{fancyhdr} % Required for custom headers
\usepackage{lastpage} % Required to determine the last page for the footer
\usepackage{extramarks} % Required for headers and footers
\usepackage[usenames,dvipsnames]{color} % Required for custom colors
\usepackage{graphicx} % Required to insert images
\usepackage{listings} % Required for insertion of code
\usepackage{courier} % Required for the courier font
\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template

% Margins
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1} % Line spacing

% Set up the header and footer
\pagestyle{fancy}
\lhead{\hmwkAuthorName} % Top left header
\chead{\hmwkClass\ (\hmwkClassInstructor\ \hmwkClassTime): \hmwkTitle} % Top center head
\rhead{\firstxmark} % Top right header
\lfoot{\lastxmark} % Bottom left footer
\cfoot{} % Bottom center footer
\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}} % Bottom right footer
\renewcommand\headrulewidth{0.4pt} % Size of the header rule
\renewcommand\footrulewidth{0.4pt} % Size of the footer rule

\setlength\parindent{0pt} % Removes all indentation from paragraphs

%----------------------------------------------------------------------------------------
%   CODE INCLUSION CONFIGURATION
%----------------------------------------------------------------------------------------

\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0} % This is the color used for comments
\lstloadlanguages{Perl} % Load Perl syntax for listings, for a list of other languages supported see: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf
\lstset{language=Perl, % Use Perl in this example
        frame=single, % Single frame around code
        basicstyle=\small\ttfamily, % Use small true type font
        keywordstyle=[1]\color{Blue}\bf, % Perl functions bold and blue
        keywordstyle=[2]\color{Purple}, % Perl function arguments purple
        keywordstyle=[3]\color{Blue}\underbar, % Custom functions underlined and blue
        identifierstyle=, % Nothing special about identifiers                                         
        commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small, % Comments small dark green courier font
        stringstyle=\color{Purple}, % Strings are purple
        showstringspaces=false, % Don't put marks in string spaces
        tabsize=5, % 5 spaces per tab
        %
        % Put standard Perl functions not included in the default language here
        morekeywords={rand},
        %
        % Put Perl function parameters here
        morekeywords=[2]{on, off, interp},
        %
        % Put user defined functions here
        morekeywords=[3]{test},
        %
        morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment
        numbers=left, % Line numbers on left
        firstnumber=1, % Line numbers start with line 1
        numberstyle=\tiny\color{Blue}, % Line numbers are blue and small
        stepnumber=5 % Line numbers go in steps of 5
}

% Creates a new command to include a perl script, the first parameter is the filename of the script (without .pl), the second parameter is the caption
\newcommand{\perlscript}[2]{
\begin{itemize}
\item[]\lstinputlisting[caption=#2,label=#1]{#1.pl}
\end{itemize}
}

%----------------------------------------------------------------------------------------
%   DOCUMENT STRUCTURE COMMANDS
%   Skip this unless you know what you're doing
%----------------------------------------------------------------------------------------

% Header and footer for when a page split occurs within a problem environment
\newcommand{\enterProblemHeader}[1]{
\nobreak\extramarks{#1}{#1 continued on next page\ldots}\nobreak
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
}

% Header and footer for when a page split occurs between problem environments
\newcommand{\exitProblemHeader}[1]{
\nobreak\extramarks{#1 (continued)}{#1 continued on next page\ldots}\nobreak
\nobreak\extramarks{#1}{}\nobreak
}

\setcounter{secnumdepth}{0} % Removes default section numbers
\newcounter{homeworkProblemCounter} % Creates a counter to keep track of the number of problems

\newcommand{\homeworkProblemName}{}
\newenvironment{homeworkProblem}[1][]{ % Makes a new environment called homeworkProblem which takes 1 argument (custom name) but the default is "Problem #"
\stepcounter{homeworkProblemCounter} % Increase counter for number of problems
\if\relax\detokenize{#1}\relax
  \renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter}} % Assign \homeworkProblemName the name of the problem
\else
  \renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter} - #1}%
\fi
\section{\homeworkProblemName} % Make a section in the document with the custom problem count
\enterProblemHeader{\homeworkProblemName} % Header and footer within the environment
}{
\exitProblemHeader{\homeworkProblemName} % Header and footer after the environment
}

\newcommand{\problemAnswer}[1]{ % Defines the problem answer command with the content as the only argument
\noindent\framebox[\columnwidth][c]{\begin{minipage}{0.98\columnwidth}#1\end{minipage}} % Makes the box around the problem answer and puts the content inside
}

\newcommand{\homeworkSectionName}{}
\newenvironment{homeworkSection}[1]{ % New environment for sections within homework problems, takes 1 argument - the name of the section
\renewcommand{\homeworkSectionName}{#1} % Assign \homeworkSectionName to the name of the section from the environment argument
\subsection{\homeworkSectionName} % Make a subsection with the custom name of the subsection
\enterProblemHeader{\homeworkProblemName\ [\homeworkSectionName]} % Header and footer within the environment
}{
\enterProblemHeader{\homeworkProblemName} % Header and footer after the environment
}

%----------------------------------------------------------------------------------------
%   NAME AND CLASS SECTION
%----------------------------------------------------------------------------------------

\newcommand{\hmwkTitle}{Assignment\ \#1} % Assignment title
\newcommand{\hmwkDueDate}{Monday,\ January\ 1,\ 2012} % Due date
\newcommand{\hmwkClass}{COMPSCI\ 101} % Course/class
\newcommand{\hmwkClassTime}{10:30am} % Class/lecture time
\newcommand{\hmwkClassInstructor}{Jones} % Teacher/lecturer
\newcommand{\hmwkAuthorName}{John Smith} % Your name

%----------------------------------------------------------------------------------------
%   TITLE PAGE
%----------------------------------------------------------------------------------------

\title{
\vspace{2in}
\textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
\normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate}\\
\vspace{0.1in}\large{\textit{\hmwkClassInstructor\ \hmwkClassTime}}
\vspace{3in}
}

\author{\textbf{\hmwkAuthorName}}
\date{} % Insert date here if you want it to appear below your name

%----------------------------------------------------------------------------------------

\begin{document}

\maketitle

%----------------------------------------------------------------------------------------
%   TABLE OF CONTENTS
%----------------------------------------------------------------------------------------

%\setcounter{tocdepth}{1} % Uncomment this line if you don't want subsections listed in the ToC

\newpage
\tableofcontents
\newpage

%----------------------------------------------------------------------------------------
%   PROBLEM 1
%----------------------------------------------------------------------------------------

% To have just one problem per page, simply put a \clearpage after each problem

\begin{homeworkProblem}[first]
Listing \ref{homework_example} shows a Perl script.

\perlscript{homework_example}{Sample Perl Script With Highlighting}

\lipsum[1]
\end{homeworkProblem}

%----------------------------------------------------------------------------------------
%   PROBLEM 2
%----------------------------------------------------------------------------------------

\begin{homeworkProblem}
\lipsum[2]

\problemAnswer{
\begin{center}
\includegraphics[width=0.75\columnwidth]{example_figure} % Example image
\end{center}

\lipsum[3-5]
}
\end{homeworkProblem}

%----------------------------------------------------------------------------------------

\end{document}

您还可以考虑添加

\usepackage{xparse}

文档序言并使用以下环境定义homeworkProblem

\NewDocumentEnvironment{homeworkProblem}{ o }{%
  \stepcounter{homeworkProblemCounter}%
  \IfValueTF{#1}
    {\renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter} - #1}}%
    {\renewcommand{\homeworkProblemName}{Part \Alph{homeworkProblemCounter}}}%
  \section{\homeworkProblemName}
  \enterProblemHeader{\homeworkProblemName}%
  }{%
    \exitProblemHeader{\homeworkProblemName}%
  }

对可选参数的条件检查更加直观。

相关内容