2

I need to create a new environment that will add a heading with an indented paragraph (the indentation applies to the entire paragraph excluding the heading). This is the desired effect:

enter image description here

So far I've come up with this. The problem is that the indentation includes the heading as well:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\newcounter{task}
\newenvironment{task}[1][]{\refstepcounter{task}\par\medskip
{\noindent\textbf{Task~\thetask #1: }}\leftskip=2em\rightskip=2em \itshape}{\par\medskip}
\begin{document}
\blindtext
\begin{task}
\blindtext
\end{task} 
\end{document}

enter image description here

Is there any way to exclude the heading from the paragraph indentation? Thanks!

3 Answers3

2

Perhaps something like this using the enumitem package:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{calc}
\usepackage{enumerate}
\usepackage{blindtext}
\newcounter{task}
\renewcommand\thetask{\textbf{Task~\arabic{task}}}
\newenvironment{task}[1][]{%
  \refstepcounter{task}%\par\medskip%
  \enumerate[labelsep=*]\item[\thetask #1:]}
  {\endenumerate}

\begin{document}
  \blindtext
  \begin{task}
  \blindtext
  \end{task}
\end{document}

The output is:

enter image description here

  • 1
    Thanks! That does the trick! The label somehow went outside the text margin, so I used align=parleft and labelindent to make it indent inside. – Oxdeadbeef Nov 01 '17 at 03:31
2

Juzst add a \hspace*{-1em}:

\documentclass[english]{article}
\usepackage{babel}
\usepackage{blindtext}

\newcounter{task}
\newenvironment{task}[1][]{\refstepcounter{task}\par\medskip
{\noindent\hspace*{-2em}\textbf{Task~\thetask #1: }}\leftskip=2em\rightskip=2em \itshape}{\par\medskip}

\begin{document}

\blindtext
\begin{task}
\blindtext
\end{task}
\blindtext

\end{document} 

enter image description here

Bernard
  • 271,350
  • Thanks, using the hspace method, I'll need to adjust it for different task environments for Hello to align vertically with the beginning of following lines. – Oxdeadbeef Nov 01 '17 at 03:33
  • @Oxdeadbef: That is a different problem you didn't explain. I'll post another code. In practice, what is the maximal value of the counter ? – Bernard Nov 01 '17 at 10:01
1

You can use the adjustwidth environment from changepage to offset paragraph text from the margins and the force a specific indentation (negative or positive) via \hspace*:

enter image description here

\documentclass{article}

\usepackage[nopar]{lipsum}
\usepackage{changepage}

\newcounter{task}
\newenvironment{task}[1][]
  {\refstepcounter{task}\par\medskip
   \begin{adjustwidth}{2em}{2em}
      \hspace*{-2em}% Remove 2em indent
      {\bfseries Task~\thetask\if\relax\detokenize{#1}\relax\else{} #1\fi:}% https://tex.stackexchange.com/q/53068/5764
      \space\itshape}
  {\end{adjustwidth}}

\begin{document}

\lipsum[1]
\begin{task}
  \lipsum[2]
\end{task}
\lipsum[3]

\end{document}

Alternatively, the same paragraph shape is obtained through a \parshape 2 specification:

\newenvironment{task}[1][]
  {\refstepcounter{task}\par\medskip
   \parshape 2 0pt \dimexpr\linewidth-2em\relax 2em \dimexpr\linewidth-4em\relax
    \noindent{\bfseries Task~\thetask\if\relax\detokenize{#1}\relax\else{} #1\fi:}
    \itshape}
  {\par}
Werner
  • 603,163