通过向您展示我的 Latex 源代码可以最好地解释我的问题:
\documentclass[10pt,a4paper]{article}
\usepackage{changepage}
\begin{document}
\section*{Homework January, 2017}
\subsubsection*{4.23}
\begin{adjustwidth}{1.2cm}{}\vspace*{-0.68cm}
A fence is 2 m tall and 3 m wide. Bla bla
$\iff (\sqrt{x + 5} - \sqrt{x + 2})^2 = 1^2$ and so on. Then also
something more in teh problem. And more and more...
\vspace*{0.15cm}
\end{adjustwidth}
Then I solve it here...
\begin{gather*}
x^2 - 3x + 2 = 1 \\
\iff (x - \frac{3}{2})^2 - \frac{9}{4} + 2 = 1 \\
\iff (x - \frac{3}{2})^2 = \frac{5}{4} \\
\iff x - \frac{3}{2} = \pm\frac{\sqrt{5}}{2} \\
\iff x = \frac{3\pm\sqrt{5}}{2} \\
x_1 = \frac{3-\sqrt{5}}{2}\\
x_2 = \frac{3+\sqrt{5}}{2}
\end{gather*}
...
\section{Homework February, 2017}
...
\end{document}
请注意,我如何使用丑陋的 vspace 命令来使段落“栅栏是...”与子节标题对齐。我的问题是,我是否可以以更“语义化”的方式实现相同的布局,而无需对测量值进行硬编码?例如,通过创建一个新的 Latex 命令,这样我就可以编写:
\problemsection{4.23}{A fence is 2 m tall ... }
Then I solve it here...
\begin{gather*}
x^2 - 3x + 2 = 1 \\
...
并且它的布局与上面的代码完全一样吗?
答案1
因此,您似乎想要一个带有悬挂缩进的带标签的段落。一种方法是:将以下几行放入您的序言中:
\newenvironment{assignment}[1]%
{\begin{description}\item[#1]}%
{\end{description}}
并像使用它一样
\begin{assignment}{4.23}
A fence is 2 m tall and 3 m wide. Bla bla
\end{assignment}
编辑:事实证明,还应该有一个包含作业条目的目录。我猜标题(“家庭作业……”)也应该反映在目录中。但是,\section*
不会添加目录条目。实现此目的的最简单方法是设置secnumdepth
为 0 并使用\section
。
\newenvironment{assignment}[1]%
{\addcontentsline{toc}{subsection}{#1}%
\begin{description}\item[#1]}%
{\end{description}}
\setcounter{secnumdepth}{0} % don't number sections
\begin{document}
\tableofcontents
\section{Homework ...}
\begin{assignment}{4.23}
...
\end{assignment}
\documentclass{article}
\usepackage{amsmath}
\newenvironment{assignment}[1]%
{\addcontentsline{toc}{subsection}{#1}%
\begin{description}\item[#1]}%
{\end{description}}
\setcounter{secnumdepth}{0} % don't number sections
\begin{document}
\tableofcontents
\section{Homework January, 2017}
\begin{assignment}{4.23}
A fence is 2 m tall and 3 m wide. Bla bla
$\iff (\sqrt{x + 5} - \sqrt{x + 2})^2 = 1^2$ and so on. Then also
something more in teh problem. And more and more...
\end{assignment}
Then I solve it here...
\begin{gather*}
x^2 - 3x + 2 = 1 \\
\iff (x - \frac{3}{2})^2 - \frac{9}{4} + 2 = 1 \\
\iff (x - \frac{3}{2})^2 = \frac{5}{4} \\
\iff x - \frac{3}{2} = \pm\frac{\sqrt{5}}{2} \\
\iff x = \frac{3\pm\sqrt{5}}{2} \\
x_1 = \frac{3-\sqrt{5}}{2}\\
x_2 = \frac{3+\sqrt{5}}{2}
\end{gather*}
...
\section{Homework February, 2017}
...
\end{document}
编辑2:assignment
为了在缺少标签的情况下获得合理的错误消息,请按如下方式更改环境的定义。
\makeatletter
\newenvironment{assignment}%
{\@ifnextchar\bgroup
{\assignmenta}%
{\GenericError{}{}{Label of assignment missing.}{}%
\assignmenta{\bfseries???}%
}
}%
{\end{description}}
\makeatother
\newcommand\assignmenta[1]%
{\addcontentsline{toc}{subsection}{#1}%
\begin{description}\item[#1]%
}
答案2
这取决于你最终想要什么,所以一开始花点心思是值得的。目前,空格和编号是手动完成的,使用偶然适合所需输出的命令。这并不是 LaTeX 的本意。
以下是一些有关语义标记的建议。根据将来的复杂程度,查看包exsheets
可能是个好主意。
注意:与往常一样,代码可以根据您的需要变得很复杂。
\documentclass{article}
\usepackage{mathtools}
\usepackage{blindtext}
\usepackage{xparse}
\usepackage{etoolbox}
\newcommand{\hwyear}{2017}
\newcounter{hwmonth}
\DeclareDocumentCommand{\nexthomeworkmonth}{ o }{
\refstepcounter{hwmonth}
\addsec{Homework \arabic{hwmonth}, \hwyear}
}
\newcounter{problemcounter}
\newbool{inproblem}
\makeatletter
\DeclareDocumentEnvironment{problem}{ o }{
\refstepcounter{problemcounter}
\booltrue{inproblem}
\par\noindent\@hangfrom{\bfseries\IfValueTF{#1}{#1}{\arabic{problemcounter}}\enspace}\ignorespaces%
}{
\par\boolfalse{inproblem}
}
\makeatother
\DeclareDocumentEnvironment{solution}{}{
\ifbool{inproblem}{\par\bigbreak\noindent\ignorespaces}{This
doesn't belong to any problem. Be careful!\par}
}{
\par
}
\begin{document}
\begin{problem}
Wombat? \blindtext
\begin{solution}
Capybara!
\end{solution}
\end{problem}
\nexthomeworkmonth
\begin{problem}[4.23]
A fence is 2 m tall and 3 m wide. Bla bla
$\iff (\sqrt{x + 5} - \sqrt{x + 2})^2 = 1^2$ and so on. Then also
something more in the problem. And more and more
\begin{solution}
Then I solve it here...\blindtext
\begin{gather*}
x^2 - 3x + 2 = 1 \\
\iff (x - \frac{3}{2})^2 - \frac{9}{4} + 2 = 1 \\
\end{gather*}
\end{solution}
\end{problem}
\nexthomeworkmonth
\begin{solution}
Duck
\end{solution}
\end{document}