假设我有以下代码
\documentclass[a4paper]{article}
\usepackage{scrextend}
\begin{document}
\begin{addmargin}[2\parindent]{0em}
expand A
\begin{addmargin}[2\parindent]{0em}
expand B
\begin{addmargin}[2\parindent]{0em}
expand C
\begin{addmargin}[2\parindent]{0em}
expand D
\end{addmargin}
expand E
\begin{addmargin}[2\parindent]{0em}
expand F
\end{addmargin}
expand G
\end{addmargin}
expand H
\begin{addmargin}[2\parindent]{0em}
expand I
\end{addmargin}
expand J
\begin{addmargin}[2\parindent]{0em}
expand K\\
expand L\\
expand M
\begin{addmargin}[2\parindent]{0em}
expand N\\
expand O\\
expand P
\end{addmargin}
\end{addmargin}
\end{addmargin}
\end{addmargin}
\end{document}
排版后如下所示:
现在,我希望我的输出有缩进标记,类似于您在此处看到的细小虚线。
我知道一些算法/伪代码环境提供了此功能,但我想要的只是纯文本。我应该怎么做?
答案1
您可以定义一个命令,按指定的宽度向右移动指定的次数,并且每次打印一个缩进标记。
\documentclass{article}
\newlength{\shiftwidth}
\setlength{\shiftwidth}{2em}
% Draw vertical rule for indentation marker
% You could also use, e.g., 0.75\baselineskip for the height
\newcommand{\indentmarker}{%
\rule{0.4pt}{2ex}
}
% Draw rule and shift right by \shiftwidth
\newcommand{\markindent}{%
\indentmarker\hspace*{\shiftwidth}%
}
% Shift right and print indentation mark specified number of times
\newcounter{shiftlevel}
\newcommand{\shift}[1]{%
\setcounter{shiftlevel}{#1}
\loop
\markindent
\addtocounter{shiftlevel}{-1}
\ifnum\value{shiftlevel} > 0
\repeat%
}
\begin{document}
Level One
\shift{1} Level Two
\shift{2} Level Three
\shift{2} More at this level
\shift{1} Level Two
Level One
\end{document}
vim
您可能会问一个单独的问题,即如果需要的话,如何在每一级换行文本(如自动缩进)。