格式化 - minipage 恢复缩进

格式化 - minipage 恢复缩进

minipage我的文档中有一些,并且parindent在此环境中自动重置。

经过一番研究,我找到了一个解决方案话题我想要改进。

我的目标

  • 按照 touhami 的建议重新定义minipage自动设置 parindent。

我的问题

  • 如何改变minipage环境(使用\makeatletter)以将前言重新定义为minipage
\saveparinfos
    \begin{minipage}{0.5\linewidth}
        \useparinfo
    \end{minipage}
  • 在环境中强制缩进是不是不好的做法minipage,在什么情况下是错误的?主题在这里已经问过这个问题,但没有答案。

  • 更全面地说,我如何/在哪里可以知道定义的环境的详细信息LaTeX以便更改它?我已经修改了包中定义的一些环境,我可以在文档中阅读代码,但对于minipage环境,我无法找到该环境的定义位置。

谢谢您的帮助,如果有英文错误请见谅。

分数维:

\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}

答案1

您可以在以下位置找到 LaTeX 的源代码来源2e。在 TeX Live 中,该命令texdoc source2e将打开相应的 PDF 文档;然后您可以使用 PDF 查看器的搜索功能或索引来查找您要查找的内容(例如,的注释源代码minipage)。

另一种方法是查看latex.ltx应该是 TeX 发行版的一部分的文件,但它没有注释。latex.ltx是许多文件的汇编,注释被删除。的源文件minipage称为ltboxes.dtx,如果您有 TeX 发行版的源代码,您可能会找到它。

如果你查看其中一个文件,你会看到,在 minipage 中,\parindent\parskip都设置为零(\@parboxrestore从 调用\@iiiminipage,它本身直接和间接地从 调用\minipage)。因此,如果你在 minipage 中看到文本缩进,则它要么出去小页面(一个框)或已在内部明确打开(当然,这两种可能性在视觉上是不同的)。

我相信以下示例的扩展版本应该可以帮助您理解这一点,并或多或少地做您想做的事情。请注意在 的定义中使用\xdef而不是。这样,您可以执行以下操作:\edef\saveparinfo

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

\useparinfo{four em}%
Etc.

否则会导致错误,因为使用\edef,用于存储已保存参数的宏只会在组内定义。\xdef使这些宏\global就像使用 定义的宏一样\newcommand。现在,我承诺的完整示例。:-)

\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}

第 1 页

第 1 页

第2页

第2页

第 3 页

第 3 页

相关内容