[Tex/LaTex] Formatting – minipage restore indent

formattingindentationminipage

I've some minipagein my document and the parindent reset automatically in this environment.

After some research, I've found a solution in this topic that I want to improve.

My goal :

  • Redefine minipage to set parindent automatically as proposed by touhami.

My questions :

  • How change the minipage environment (with \makeatletter) to redefine preamble of minipage as :
\saveparinfos
    \begin{minipage}{0.5\linewidth}
        \useparinfo
    \end{minipage}
  • Is it bad use to force indent in minipage environment and in which case is it wrong ? The topic here had already asked this question but there are no answer.

  • More globally, how/where can I know the details of an environment defined in LaTeX in order to change it ? I've already modify some environment defined in package where i could read the code in the documentation but for minipage environment, I'm not able to find where this environment is defined.

Thank you for you help and sorry if there is some english mistakes.

MWE :

\documentclass[11pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{lipsum}

%%Command to set indentation in minipage
\newcommand{\saveparinfos}{%
    \edef\myindent{\the\parindent}%
    \edef\myparskip{\the\parskip}}


\newcommand{\useparinfo}{%
    \setlength{\parindent}{\myindent}%
    \setlength{\parskip}{\myparskip}}


\begin{document}
\section{My section}
\subsection{Example of indented text and non-indented in minipage}

    \lipsum[1-2]
    \vspace{0.2cm}

    \noindent\saveparinfos\begin{minipage}{0.48\linewidth}
        This is a long text to fill the line untill a linebreak appears.

        And there is no indent for each paragraph.
    \end{minipage}\hfill
    \begin{minipage}{0.48\linewidth}\useparinfo
        \lipsum[2]

        And this minipage has indentation for each paragraph.
    \end{minipage}
\end{document}

Best Answer

You can find the source code of LaTeX in source2e. In TeX Live, the command texdoc source2e opens the corresponding PDF document; then you can use the search function of your PDF viewer or the index to find what you are looking for (e.g., commented source code for minipage).

Another way is to look at the file latex.ltx that should be part of your TeX distribution, but it has no comments. latex.ltx is the compilation of many files, with comments removed. The source file for minipage is called ltboxes.dtx, you may have it if you have source code for your TeX distribution.

If you look in one of these files, you'll see that \parindent and \parskip are both set to zero inside a minipage (\@parboxrestore is called from \@iiiminipage, which is itself called directly and indirectly from \minipage). Therefore, if you see indentation for text inside a minipage, it is either out of the minipage (which is a box) or has been switched on explicitly inside (these two possibilities are visually different, of course).

I believe the following expanded version of your example should help you understand this and do more or less what you want. Note the use of \xdef instead of \edef inside the definition of \saveparinfo. This way, you can do things like

{%
  \parindent=4em\relax
  \saveparinfo{four em}%
  Foo bar.\par}

\useparinfo{four em}%
Etc.

which would otherwise cause an error, because with \edef, the macros used to store the saved parameters would only be defined inside the group. \xdef makes these macros \global, just like macros defined with \newcommand. Now, the full example I promised. :-)

\documentclass[11pt,a4paper]{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{lipsum}
\usepackage{calc}

\makeatletter

% Command to save \parindent and \parskip under a specific key. The argument
% is the key and can even contain non-letters (e.g., hyphens or spaces).
\newcommand*{\saveparinfo}[1]{%
  \expandafter\xdef\csname my@#1@parindent\endcsname{\the\parindent}%
  \expandafter\xdef\csname my@#1@parskip\endcsname{\the\parskip}%
}

% Restore parameters that were saved under the specified key.
\newcommand*{\useparinfo}[1]{%
  \parindent=\csname my@#1@parindent\endcsname \relax
  \parskip=\csname my@#1@parskip\endcsname \relax
}

\makeatother

% Like minipage, but suppresses indentation if used in vertical mode (i.e.,
% before a paragraph has been started).
\newenvironment{myMinipage}{%
  % If we are about to start a paragraph, the indentation box will be *out*
  % of the minipage -> empty it first.
  \noindent
  \minipage    % like \begin{minipage} but gives nicer error handling here
}{%
  \endminipage % ditto for \end{minipage}
}

\newcommand*{\nextSample}{\bigskip\filbreak}


\begin{document}
\section{My section}
\subsection{Example of indented text and non-indented in minipage}

\lipsum[1-2]
\bigskip

\parindent=0pt\relax % Suppress paragraph indentation and save parameters
\saveparinfo{zero}%
No indentation. We saved the \verb|\parindent| and \verb|\parskip| parameters
under key \texttt{zero}.
\nextSample

\begin{myMinipage}{\linewidth}
  This is a long text to fill the line until a linebreak appears. Bla bla
  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla.

  And there is no indentation for any paragraph.
\end{myMinipage}
\nextSample

\parindent=1em\relax
\saveparinfo{oneem}%
Now, paragraphs are indented. We saved the \verb|\parindent| and
\verb|\parskip| parameters under key \texttt{oneem}. Bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla bla.

Another paragraph with the same parameters. Bla bla bla bla bla bla bla
bla bla bla bla bla bla bla bla bla bla bla.
\nextSample

\begin{myMinipage}{\linewidth}
  Here, paragraphs are not indented. \lipsum[1]

  Other paragraph inside the minipage. No indentation.
\end{myMinipage}
\nextSample

\begin{minipage}{\linewidth-\parindent} % real minipage environment
  Here, the whole minipage is preceded by an indentation box, but there is
  no indentation for paragraphs \emph{inside} the minipage.

  \lipsum[1]
\end{minipage}
\nextSample

\begin{myMinipage}{\linewidth}\useparinfo{zero}%
  This minipage starts at the left margin and has no indentation for its
  paragraphs (this was restored using key \texttt{zero}).

  \lipsum[1]
\end{myMinipage}
\nextSample

\begin{myMinipage}{\linewidth}\useparinfo{oneem}%
  This minipage starts at the left margin and has one em indentation for
  each paragraph (this was restored using key \texttt{oneem}).

  \lipsum[1]
\end{myMinipage}
\end{document}

Page 1

Page 1

Page 2

Page 2

Page 3

Page 3

Related Question