# Having the first paragraph line up next to the section heading

My problem is best explained by showing you my Latex source:

\documentclass[10pt,a4paper]{article}
\usepackage{changepage}
\begin{document}
\section*{Homework January, 2017}
\subsubsection*{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...
\vspace*{0.15cm}
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}


Note how I'm using ugly vspace commands to get the paragraph "A fence is ..." to line up next to the subsection heading. My question is if I can achieve the same layout in a more "semantic" way, without hardcoding measurements? For example, by creating a new Latex command so that I can write:

\problemsection{4.23}{A fence is 2 m tall ... }
Then I solve it here...
\begin{gather*}
x^2 - 3x + 2 = 1 \\
...


and have it being layouted exactly as the above code?

• Please do not post fragments, but compilable minmal working examples. – TeXnician Mar 19 '17 at 11:39
• Floating is misleading here, in my point of view. Especially if you provide the problem number manually -- a floating 'section' would scramble the order of appearance.... – user31729 Mar 19 '17 at 11:41
• You're right. I've changed the question title. – Björn Lindqvist Mar 19 '17 at 11:46

So it seems you want to have a labeled paragraph with hanging indentation. One way to do it: Put the following lines into your preamble:

\newenvironment{assignment}%
{\begin{description}\item[#1]}%
{\end{description}}


and use it like

\begin{assignment}{4.23}
A fence is 2 m tall and 3 m wide. Bla bla
\end{assignment}


Edit: It turns out that there should also be a table of contents containing entries for the assignments. I guess that also the headlines ("Homework ...") should be reflected in the toc. However, \section* does not add toc entries. The easiest way to achieve this is to set secnumdepth to 0 and to use \section.

\newenvironment{assignment}%
\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}%
\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}


Edit 2: To get a reasonable error messages in case of a missing label change the definition of the assignment environment as follows.

\makeatletter
\newenvironment{assignment}%
{\@ifnextchar\bgroup
{\assignmenta}%
{\GenericError{}{}{Label of assignment missing.}{}%
\assignmenta{\bfseries???}%
}
}%
{\end{description}}
\makeatother
\newcommand\assignmenta%
\begin{description}\item[#1]%
}

• Not bad, but the paragraph "A fence is 2 m.." doesn't line up as it does in my code. – Björn Lindqvist Mar 19 '17 at 12:33
• @BjörnLindqvist Nothing lines up in your code, it is not compilable. – Johannes_B Mar 19 '17 at 12:37
• @BjörnLindqvist I have revised my answer. This kind of alignment? – gernot Mar 19 '17 at 12:51
• Exactly! I didn't know it was called hanging indentation. But now there is no entry created for the section in the table of contents. – Björn Lindqvist Mar 19 '17 at 13:04
• @BjörnLindqvist Well, you didn't indicate that you want to have a table of contents. Quite the opposite, since you use \section* which does not write toc entries either. See my modified answer. – gernot Mar 19 '17 at 13:15

It depends what you want in the end, so a bit of thought at the beginning might be worth your while. Right now, spacing and numbering is done by hand, using commands that suit by accident the wanted output. Not really the way LaTeX was meant.

Here a few suggestions with semantic markup. Depending how complex it will get in the future, a look into package exsheets might be a good idea.

Note: As always, the code can be as complex as you want it to be. \documentclass{article}
\usepackage{mathtools}
\usepackage{blindtext}
\usepackage{xparse}
\usepackage{etoolbox}
\newcommand{\hwyear}{2017}
\newcounter{hwmonth}
\DeclareDocumentCommand{\nexthomeworkmonth}{ o }{
\refstepcounter{hwmonth}
}
\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}