TransWikia.com

Indent a problem description defined by newenvironment

TeX - LaTeX Asked by ferris on May 16, 2021

I am having trouble figuring out how to indent text in a custom environment

newenvironment{problem}[2][Problem]{begin{trivlist}
item[hskip labelsep {bfseries #1}hskip labelsep {bfseries #2.}]}{end{trivlist}}

Currently, the text description of each problem does not indent. What I wish to achieve is for every new line of text to "align" with the red dashed line shown in this picture.

Is there a clean way to modify the code in order to achieve this?
enter image description here

To illustrate another example, I wish to make the Problem environment have an indentation feature similar to the subsection environment:
enter image description here

2 Answers

You can use the standard list environment.

documentclass{article}
usepackage{lipsum}

newlength{problemmargin}
newenvironment{problem}[2][Problem]
 {%
  settowidth{problemmargin}{hspace{labelsep}bfseries #1 #2.}%
  begin{list}{}{leftmargin=problemmargin labelwidth=leftmargin}
  item[bfseries #1 #2.]%
 }
 {end{list}}

begin{document}

lipsum[3]

begin{problem}{21}
lipsum[1][1-4]
end{problem}

lipsum[4]

begin{problem}{22}
Solve the following equations
begin{enumerate}
item A
item B
item C
end{enumerate}
and check the values.
end{problem}

lipsum[4]

end{document}

enter image description here

Correct answer by egreg on May 16, 2021

Here is a solution, based on the description environment, using the tools of`enumitem. As you can see, it breaks across pages:

documentclass{book}
usepackage{enumitem}
setlengthparindent{1em}
newlength{descrwidth}
newenvironment{problem}[2][Problem]%
{settowidth{descrwidth}{textbf{#1 #2}hspace*{2labelsep}}begin{description}[font=bfseries, labelindent=labelsep, leftmargin =descrwidth]
item[#1hspace*{ labelsep}#2.]}%
{end{description}}%

usepackage{lipsum}
pagestyle{plain}

begin{document}

lipsum[2-6]

begin{problem}{21}%[noitemsep, labelwidth=dimexprdescrwidth, leftmargin =descrwidth, labelsep=0pt]
lipsum[11]
end{problem} 

lipsum[9]

end{document} 

enter image description here

Answered by Bernard on May 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP