将内容(例如 fancyhdr 的标题)与右列水平对齐

将内容(例如 fancyhdr 的标题)与右列水平对齐

我还没有看到过有人在这里问这个问题,但我仍然认为对于那些试图创建稍微复杂一点的布局的人来说,这可能会很有用。

这个想法很简单,我试图将内容与我的multicols环境的第二列对齐。

在下面的示例中,我以标题的形式包含了此类内容的示例,通过手动调整尺寸。显然,如果您想设置不同的尺寸\columnsep、更改其中一列的大小等,这会妨碍工作流程。

为了使示例更清晰一些,我添加了一个图像(附带编译同一文档的代码)。标题的内容以及列的内容都应水平对齐,但我在这里完成的方式需要手动调整尺寸。

有什么更好的做法(和/或更简单)来获得这种结果?

http://i.imgur.com/JRhEavq.png

\documentclass[twoside]{article}
\usepackage{multicol,fancyhdr}

% sample text
\def\sA{Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.}%
\def\sB{Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war.}%
\def\sC{We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.}%
\def\sD{It is altogether fitting and proper that we should do this.}%
\def\sE{But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground.}%
\def\sF{The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.}%
\def\sG{The world will little note, nor long remember what we say here, but it can never forget what they did here.}%
\def\sH{It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.}%
\def\sI{It is rather for us to be here dedicated to the great task remaining before us―--that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion―that we here highly resolve that these dead shall not have died in vain―that this nation, under God, shall have a new birth of freedom―and that government of the people, by the people, for the people, shall not perish from the earth.}%
\edef\txt{\sA\ \sB\ \sC\ \sD\ \sE\ \sF\ \sG\ \sH\ \sI\ }
\edef\Txt{\txt \txt \txt}

% something to mimic the desired result visually
\columnsep=50pt%
\fancypagestyle{x}{%
\fancyhf{}%
\renewcommand{\headrulewidth}{0pt}%
\fancyhead[L]{%
\hbox to 197pt{\hfill \rlap{\leftmark}}% I just guessed this value
}%
}%

\begin{document}
\pagestyle{x}
\begin{multicols}{2}[\section{My example section}]
\Txt
\end{multicols}
\end{document}

答案1

您可以在 中使用 a \parbox(或\makebox,但\parbox更好)来代替。\columnwidth\fancyhead[R]\fancyhead[L]

\fancypagestyle{x}{%
\fancyhf{}%
\renewcommand{\headrulewidth}{0pt}%
\fancyhead[R]{%
\parbox{\columnwidth}{\leftmark}% no guessing
}%
}%

完整代码:

\documentclass[twoside]{article}
\usepackage{multicol,fancyhdr}

% sample text
\def\sA{Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.}%
\def\sB{Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war.}%
\def\sC{We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live.}%
\def\sD{It is altogether fitting and proper that we should do this.}%
\def\sE{But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground.}%
\def\sF{The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.}%
\def\sG{The world will little note, nor long remember what we say here, but it can never forget what they did here.}%
\def\sH{It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.}%
\def\sI{It is rather for us to be here dedicated to the great task remaining before us―--that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion―that we here highly resolve that these dead shall not have died in vain―that this nation, under God, shall have a new birth of freedom―and that government of the people, by the people, for the people, shall not perish from the earth.}%
\edef\txt{\sA\ \sB\ \sC\ \sD\ \sE\ \sF\ \sG\ \sH\ \sI\ }
\edef\Txt{\txt \txt \txt}

% something to mimic the desired result visually
\columnsep=50pt%
\fancypagestyle{x}{%
\fancyhf{}%
\renewcommand{\headrulewidth}{0pt}%
\fancyhead[R]{%
\parbox{\columnwidth}{\leftmark}% % no guessing
}%
}%

\begin{document}
\pagestyle{x}
\begin{multicols}{2}[\section{My example section}]
\Txt
\end{multicols}
\end{document}

在此处输入图片描述

相关内容