我想创建一个宏来创建一个包含两列的表格。一个内容应该放在内列中,另一个内容应该放在外列中,无论内列是左列还是右列,因此:
\newcommand{\testmacro}{
% On odd pages:
\begin{tabular}{lr}Inner text & Outer text\end{tabular}
% On even pages:
\begin{tabular}{lr}Outer text & Inner text\end{tabular}
}
有办法实现这个吗?
答案1
不幸的是@lziminza 的建议可能失败:
\documentclass[twoside]{article}
\usepackage{ifoddpage}
\newcommand{\testmacro}{%
\checkoddpage
\ifoddpage
\begin{tabular}{lr}Inner text & Outer text\end{tabular}
\else
\begin{tabular}{lr}Outer text & Inner text\end{tabular}
\fi
}
\usepackage{blindtext}
\begin{document}
\blindtext[2]
\vskip 4\baselineskip
\blindtext[2]
\testmacro
\blindtext
\testmacro
\end{document}
结果是:
经过两次或多次 LaTeX 运行后,出现在第二页上。
为了避免此问题,您至少应禁止在\checkoddpage
和之间分页\ifoddpage
。我建议还明确允许在 之前分页\checkoddpage
:
\documentclass[twoside]{article}
\usepackage{ifoddpage}
\newcommand{\testmacro}{%
% \par % could also be a good idea
\pagebreak[1]
\checkoddpage\nobreak
\ifoddpage
\begin{tabular}{lr}Inner text & Outer text\end{tabular}
\else
\begin{tabular}{lr}Outer text & Inner text\end{tabular}
\fi
}
\usepackage{blindtext}
\begin{document}
\blindtext[2]
\vskip 4\baselineskip
\blindtext[2]
\testmacro
\blindtext
\testmacro
\end{document}
结果是:
在第二页。
另一个建议是设置测试标签里面并tabular
在外面做测试:
\documentclass[twoside]{article}
\usepackage{refcount}
\newcounter{testpage}
\newcommand{\testmacro}{%
\ifodd \getpagerefnumber{testpage:\thetestpage}
\begin{tabular}{lr}\label{testpage:\thetestpage}\stepcounter{testpage}Inner text & Outer text\end{tabular}
\else
\begin{tabular}{lr}\label{testpage:\thetestpage}\stepcounter{testpage}Outer text & Inner text\end{tabular}
\fi
}
\usepackage{blindtext}
\begin{document}
\blindtext[2]
\testmacro
\vskip 3\baselineskip
\blindtext[2]
\testmacro
\blindtext
\testmacro
\end{document}
也有效:
答案2
使用\usepackage{ifoddpage}
包。
\newcommand{\testmacro}{
\checkoddpage
\ifoddpage
\begin{tabular}{lr}Inner text & Outer text\end{tabular}
\else
\begin{tabular}{lr}Outer text & Inner text\end{tabular}
\fi
}