一行可以按如下方式对齐:\makebox[\textwidth][s]{<text>}
。当宽度未知时,如何仅对齐一行的一部分?当然,我可以通过反复试验来调整宽度,但我不想这样做。
下面描述了用例。
我使用exsheets
包并将wrapfig
图形嵌入到练习中。
练习 1 表示我通常试图避免的情况,即图形上方没有文本。
练习 2、3 表示通常情况下图形上方有一两行。有时看起来还不错,但总是需要大量手动工作。我需要找到第一行的断点,并且需要使第二行对齐\makebox[][s]{}
。
我有不同类型的练习,具有不同的邮政编码(以方括号显示)。
在练习编号后使用\hfill
后置代码来结束段落正常行结束的位置,否则可能会结束得太早。但在某些情况下\hfill
会产生太多空间。另一个选择是使用\makebox[<width>][s]{<text>}
,但由于带有后置代码的问题编号长度可变,我需要根据每个问题进行调整。
如果有更合适的软件包和方法,我也想了解。
\documentclass[a4paper,14pt]{extbook}
\usepackage[english,latin]{babel}
\usepackage{wrapfig} \setlength{\intextsep}{0pt}
\usepackage{graphicx} \graphicspath{{plots/}}
\usepackage[auto-label,counter-within=chapter]{exsheets}
\SetupExSheets{headings=runin}
\DeclareInstance{exsheets-heading}{runin}{default}{
runin = true,
number-pre-code = \hspace{1em},
number-post-code =~,
join = { main[hc,vc]number[l,vc](0em,0pt) }
}
\begin{document}
~
\begin{wrapfigure}[]{r}{0.39\textwidth}
\includegraphics[width=0.94\linewidth]{3_1_13}
\end{wrapfigure}
\begin{question}[ID=1]
\texttt{[some text here]}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{question}
\noindent\parbox{\linewidth}{
\begin{question}[ID=2]
\texttt{[another text]}
%Lorem ipsum dolor sit amet, consectetur % is not justified
%\hfill Lorem ipsum dolor sit amet, consectetur % too much space before "Lorem"
\makebox[0.675\textwidth][s]{ Lorem ipsum dolor sit amet, consectetur} %need to guess the multiplier of the \textwidth
\begin{wrapfigure}[]{r}{0.39\textwidth}
\includegraphics[width=0.94\linewidth]{3_1_13}
\end{wrapfigure}
\noindent \mbox{adipiscing} elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{question}
}
\noindent\parbox{\linewidth}{
\begin{question}[ID=3]
\texttt{[something else]}
\hfill Lorem ipsum dolor sit amet, consectetur adi- % this particular case looks OK, but I'd prefer to use \makebox[<width>][s]{<text>}.
\makebox[\textwidth][s]{piscing, sed do eiusmod tempor incididunt ut labore et magna. Ut enim}
\begin{wrapfigure}[]{r}{0.39\textwidth}
\includegraphics[width=0.94\linewidth]{3_1_13}
\end{wrapfigure}
\noindent Ut enim ad enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{question}
}
\end{document}
答案1
此解决方案创建环境myformat
以将保存框中\illustration
指定的行数放入文本中。
值 2.2em 大约是问题所指的缩进量。我真的不想深入研究 exsheets 或 exbook 来确定精确的宽度。
\documentclass[a4paper,14pt]{extbook}
\usepackage[english,latin]{babel}
\usepackage{graphicx} \graphicspath{{plots/}}
\usepackage{tikz}
\usepackage{lipsum}% MWE only
\usepackage{showframe}% MWE only
\usepackage[auto-label,counter-within=chapter]{exsheets}
\SetupExSheets{headings=runin}
\DeclareInstance{exsheets-heading}{runin}{default}{
runin = true,
number-pre-code = \hspace{1em},
number-post-code =~,
join = { main[hc,vc]number[l,vc](0em,0pt) }
}
\newsavebox{\illustration}% used to pass image
\newenvironment{myformat}[1]{% #1=line number for start of illustration (1=first)
\begin{minipage}[t]{\dimexpr \linewidth-2.2em}% kludge
\setlength{\dimen0}{\dimexpr \textwidth-\wd\illustration-\columnsep}% reduced line width
\setlength{\dimen1}{\dimexpr \ht\illustration + \dp\illustration}% total height
\count1=\numexpr \dimen1/\baselineskip\relax
\ifdim \dimen1>\the\count1\baselineskip\relax
\advance\count1 by 1
\fi
\count2=0% construct \parshape arguments
\ifnum #1<2\relax
\edef\shape{\the\numexpr \count1+1}%
\else
\edef\shape{\the\numexpr \count1+#1}%
\loop\advance\count2 by 1
\ifnum\count2<#1\relax
\edef\shape{\shape\space 0pt \the\linewidth}%
\repeat
\count2=0
\fi
\loop\ifnum\count2<\count1
\edef\shape{\shape\space 0pt \the\dimen0}%
\advance\count2 by 1
\repeat
\edef\shape{\shape\space 0pt \the\linewidth}%
\parshape=\shape
\tikz[overlay]{\path ({\textwidth-\wd\illustration},{\ht\strutbox+\baselineskip-#1\baselineskip})
node[below right, inner sep=0pt]{\usebox\illustration};}%
\ignorespaces}{\end{minipage}}
\begin{document}
\savebox{\illustration}{\includegraphics[width=0.2\textwidth]{example-image}}%
\begin{question}[ID=1] \begin{myformat}{1}
\texttt{[some text here]}
\LipsumPar{1}
\end{myformat}
\end{question}
\begin{question}[ID=2] \begin{myformat}{2}
\texttt{[another text]}
\LipsumPar{1}
\end{myformat}
\end{question}
\begin{question}[ID=3] \begin{myformat}{3}
\texttt{[something else]}
\LipsumPar{1}
\end{myformat}
\end{question}
\end{document}
除了 TikZ 之外,还可以使用
\leavevmode\rlap{\hspace{\dimexpr \textwidth-\wd\illustration}%
\raisebox{\dimexpr \ht\strutbox+\baselineskip-#1\baselineskip-\ht\illustration}[0pt][0pt]%
{\usebox\illustration}}%
我将它们开发为完全独立的环境,然后弄清楚如何合并大部分代码。
棘手的是question
包含\par
和\parshape
必须位于之后\par
但位于其他内容之前。因此,就像wrapfig
我使用的一样\everypar
。
此版本不再需要缩进question
,而是需要它使用的额外垂直间距(近似值)。
\documentclass[oneside,12pt,a4paper]{book}
\usepackage[english,latin]{babel}
\usepackage{graphicx} \graphicspath{{plots/}}
\usepackage{lipsum}% MWE only
\usepackage{showframe}% MWE only
\usepackage{hyperref}
\usepackage[auto-label,counter-within=chapter]{exsheets}
\SetupExSheets{headings=runin}
\DeclareInstance{exsheets-heading}{runin}{default}{
runin = true,
number-pre-code = \hspace{1em},
number-post-code =~,
join = { main[hc,vc]number[l,vc](0em,0pt) }
}
\newsavebox{\illustration}% used to pass image
\newenvironment{leftfigure}[1]{% #1=line number for start of illustration (1=first)
\setlength{\dimen2}{\dimexpr \wd\illustration+\columnsep}% indentation
\setlength{\dimen0}{\dimexpr \linewidth-\dimen2}% reduced line width
\setlength{\dimen1}{\dimexpr \ht\illustration + \dp\illustration}% total height
\setlength{\dimen3}{0pt}% image offset
\commonfigure{#1}}{}
\newenvironment{rightfigure}[1]{% #1=line number for start of illustration (1=first)
\setlength{\dimen3}{\dimexpr \linewidth-\wd\illustration}% offset to image
\setlength{\dimen0}{\dimexpr \dimen3-\columnsep}% reduced line width
\setlength{\dimen1}{\dimexpr \ht\illustration + \dp\illustration}% total height
\setlength{\dimen2}{0pt}% indentation
\commonfigure{#1}}{}
\newcommand{\commonfigure}[1]{% combine environments
\count1=\numexpr \dimen1/\baselineskip\relax
\ifdim \dimen1>\the\count1\baselineskip\relax
\advance\count1 by 1
\fi
\count2=0% construct \parshape arguments
\ifnum #1<2\relax
\edef\shape{\the\numexpr \count1+1}%
\else
\edef\shape{\the\numexpr \count1+#1}%
\loop\advance\count2 by 1
\ifnum\count2<#1\relax
\edef\shape{\shape\space 0pt \the\linewidth}%
\repeat
\count2=0
\fi
\loop\ifnum\count2<\count1
\edef\shape{\shape\space \the\dimen2\space \the\dimen0}%
\advance\count2 by 1
\repeat
\edef\shape{\shape\space 0pt \the\linewidth}%
\par\noindent\rlap{\hspace{\dimen3}% overlap illustration
\raisebox{\dimexpr \ht\strutbox+\baselineskip-#1\baselineskip-\ht\illustration-0.8ex}% 0.8ex fudge
[0pt][0pt]{\usebox\illustration}}%
\strut\vspace{-\baselineskip}% approximate the vertical spacing for question
\everypar{\parshape=\shape\everypar{}}%
}
\begin{document}
\savebox{\illustration}{\includegraphics[width=0.2\textwidth]{example-image}}%
\begin{rightfigure}{1}
\begin{question}[ID=1]
\texttt{[some text here]}
\LipsumPar{1}
\end{question}
\end{rightfigure}
\begin{rightfigure}{2}
\begin{question}[ID=2]
\texttt{[another text]}
\LipsumPar{1}
\end{question}
\end{rightfigure}
\begin{rightfigure}{3}
\begin{question}[ID=3]
\texttt{[something else]}
\LipsumPar{1}
\end{question}
\end{rightfigure}
\begin{leftfigure}{1}
\begin{question}[ID=4]
\texttt{[some text here]}
\LipsumPar{1}
\end{question}
\end{leftfigure}
\begin{leftfigure}{2}
\begin{question}[ID=5]
\texttt{[another text]}
\LipsumPar{1}
\end{question}
\end{leftfigure}
\begin{leftfigure}{3}
\begin{question}[ID=6]
\texttt{[something else]}
\LipsumPar{1}
\end{question}
\end{leftfigure}
\end{document}