26

Assume I want to have a section within an article with different indentation/parskipping. Is there a way to undo that command at the end of the section so the next section behaves again as if I never used a setlength on parindent?

I mean I could just use the \setlength{\parindent}{}-command again with the default parameters but what if I don't know the standard parameters of latex?

\documentclass{article}
\usepackage{ngerman, blindtext}
\begin{document}
\section{Section with different settings}

\setlength{\parindent}{0cm}\setlength{\parskip}{4ex plus 0.3ex minus 0.1ex}

\blindtext\par\blindtext
%Here I would want to undo the setlength command from above and go back to default
\section{Section back to default settings}

\blindtext\par\blindtext
\end{document}
lockstep
  • 250,273
Philipp
  • 5,078
  • 10
  • 30
  • 32

2 Answers2

28

Enclose the section in question within a group.

\documentclass{article}
\usepackage{ngerman, blindtext}
\begin{document}

\begingroup
\section{Section with different settings}

\setlength{\parindent}{0cm}\setlength{\parskip}{4ex plus 0.3ex minus 0.1ex}

\blindtext\par\blindtext
\endgroup

%Here I would want to undo the setlength command from above and go back to default
\section{Section back to default settings}

\blindtext\par\blindtext
\end{document}
lockstep
  • 250,273
18

A different way than using groups can come handy in certain situations:

\newcommand{\keepvalues}{%
  \edef\restorevalues{%
    \parindent=\the\parindent
    \parskip=\the\parskip
  }%
}

Then you can say

\keepvalues

\section{Section with different settings}

\setlength{\parindent}{0cm}
\setlength{\parskip}{4ex plus 0.3ex minus 0.1ex}

<text>

\restorevalues

\section{Section back to default settings}

A more generic approach can be giving \keepvalues a list of length parameters to keep:

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\keepvalues}{ O{\parindent,\parskip} }
  {
   \tl_clear:N \l_tmpa_tl
   \clist_map_inline:nn { #1 }
     { \tl_put_right:Nx \l_tmpa_tl { ##1 = \skip_use:N ##1 } }
   \cs_set:Npx \restorevalues { \tl_use:N \l_tmpa_tl }
  }
\ExplSyntaxOff

\begin{document}

\keepvalues[\parindent,\parskip,\leftmargini]

\section{Section with different settings}

\setlength{\parindent}{0cm}
\setlength{\parskip}{4ex plus 0.3ex minus 0.1ex}
\addtolength{\leftmargini}{2em}

<text>

\restorevalues

\section{Section back to default settings}

The call \keepvalues is equivalent to \keepvalues[\parindent,\parskip]. In the argument can go a list of parameters which can be set with \setlength.

No nested calls, of course.

egreg
  • 1,121,712