6

I want to create an environment that will have indentation to all the content inside it. Similar to \begin{enumberate}- \end{enumerate} but without need of writing \item

I tried doing it like this

\newenvironment{indented}
{\par\leftskip1cm\relax}
{\par\leftskip0cm\relax}

This works partially with 2 problems -

  1. Nesting doesn't work.
  2. Code listing using lstlisting doesn't honor this.

Please suggest how to make this work.

3
  • Nesting can be done with \newenvironment{indented} {\par\leftskip=\dimexpr\leftskip+1cm\relax} {\par\leftskip=\dimexpr\leftskip-1cm\relax}. However, it will not address the lstlisting issue. Dec 24, 2021 at 4:19
  • 1
    @StevenB.Segletes why not \advance\leftskip 1cm\relax?
    – Skillmon
    Dec 27, 2021 at 21:34
  • @Skillmon Thank you for the more efficient syntax. My plain TeX skills are somewhat rudimentary. Dec 27, 2021 at 22:22

2 Answers 2

6

adjustwidth from changepage is an environment that allows you to adjust the left and right margin. Here's a simple implementation (using the base code from Niranjan's answer):

enter image description here

\documentclass{article}

\usepackage{changepage}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\newenvironment{enumlike}[1][15pt]{%
  \begin{adjustwidth}{#1}{0pt}% Only indent on left side
}{%
  \end{adjustwidth}%
}

\begin{document}

\begin{enumlike}

  \begin{lstlisting}
#!/bin/usr
echo "hello world"
  \end{lstlisting}
  
  abcd efgh

  abcd efgh
  \begin{enumlike}[20pt]
    \begin{lstlisting}
#!/bin/usr
echo "hello world"
    \end{lstlisting}

    pqrs wxyz
    
    pqrs wxyz
  \end{enumlike}
\end{enumlike}

\end{document}

Note that 15pt - the default optional argument for enumlike - is the default \parindent, which could make things line up coincidently. Of course, this can be changed.

6
  • Hmm, this is even better :)
    – Niranjan
    Dec 24, 2021 at 5:22
  • This works great, but is not able to handle float environments like \begin{table}-\end{table} and \begin{figure}-\end{figure}, can we modify it to support these float environments.
    – aaryan
    Jan 10 at 5:10
  • @aaryan: Floats float, which means they would not stay within the scope of the width changes. If you want that to occur, try with \begin{figure}[H] and \begin{table}[H] (you'll need to add \usepackage{float} to your preamble).
    – Werner
    Jan 10 at 6:16
  • @Werner Yes, I am not talking about floating of the floats. When I use [H] they stay in place but the width is not affected by the parent enumlike environment. The table starts from the left margin of the page which I don't want. It should start from the most recent indentation.
    – aaryan
    Jan 10 at 11:24
  • @aaryan: Floats use a different setup, and specifically set their content inside a box of width \columnwidth, not \linewidth. Apart from that, horizontal alignment in also set differently. I'd suggest asking a new (follow-up) question.
    – Werner
    Jan 11 at 0:44
2

Why not to use the quote environment instead?

\documentclass{article}
\usepackage{listings}
\newenvironment{enumlike}{%
  \begin{quote}%
  }{%
  \end{quote}%
}

\begin{document}
\begin{enumlike}
  \begin{lstlisting}
#!/bin/usr
echo "hello world"
  \end{lstlisting}
  
  abcd efgh

  abcd efgh
  \begin{enumlike}
    \begin{lstlisting}
#!/bin/usr
echo "hello world"
    \end{lstlisting}

    pqrs wxyz
    
    pqrs wxyz
  \end{enumlike}
\end{enumlike}
\end{document}

1

2
  • But quote indents at both sides. Dec 24, 2021 at 5:49
  • Right, my bad. Werner's solution is better.
    – Niranjan
    Dec 24, 2021 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.