1

I defined a command that contains two different tabulars, when I use book.cls, I want to use this command in odd and even pages.

How can this command test if the page is odd or even to change the two tabular position?

I don't know how to define this command, just like \newcommand{}{}.

=====================================================

As my expression not clear, I have changed the codes. Is there one method, add some check command in \newcommand{}{}, so when use \showtab, let it auto check the current page number is odd or even, so it will change the two tabular positon.

I have just give simple codes to show what I want.

\documentclass[]{article}

\usepackage{colortbl}
\usepackage[table]{xcolor}


\newcommand{\showtab}{\begin{flushleft}
        \begin{minipage}[t]{0.4\linewidth}
            \begin{tabular}[t]{|p{\linewidth}|}
                \hline
                text text text text text\\
                \hline
            \end{tabular}
        \end{minipage}\hfill
        \begin{minipage}[t]{0.5\linewidth}
            \begin{tabular}[t]{@{}p{\linewidth}@{}}
                \hline
                text text text text text text text text text text text text text text text text\\
                \hline
                text text text text text text text text\\
                \hline
                text text text text text text text text\\
                \hline
            \end{tabular}
        \end{minipage}
\end{flushleft}}


\begin{document}



\showtab
\newpage

\showtab
\newpage



\end{document}

enter image description here enter image description here

zongxian
  • 467
  • 1
    Can somebody remove the 'tex-core' tag when reviewing the edit? I missed it and now can't edit until that edit is approved or rejected. – cfr Jun 14 '18 at 02:48
  • Please don't down-vote without leaving a comment explaining how the question can be improved. And I don't see anything much to complain of here, except that the English is poor, which probably just means the OP isn't an English speaker. – cfr Jun 14 '18 at 02:51
  • 1
    This is not going to go well. If you test for the page, but the table goes to the next page, you'll end up with the wrong format. So, at a minimum, you'd have to figure out if there is space on the current page, start a new page if not, and then figure out which format to use. (Or let the new page start itself and flip the test.) And you risk a lot of white space, too, by using non-floating tables. Is that what you want? Is this a special use (e.g. a form) where usual considerations may be less applicable? – cfr Jun 14 '18 at 02:55
  • can you show your command? for this what you like to obtain you need to test if \pageref{...} to your command gives odd or even page. for this you need compile your document at least twice. – Zarko Jun 14 '18 at 05:41
  • Did you really just upload an image of code? – Johannes_B Jun 14 '18 at 06:10
  • @Johannes_B image is what I want achieve, codes just do the odd page thing, so I want to add some codes to show even page thing. – zongxian Jun 14 '18 at 06:14
  • 1

2 Answers2

3

Odd/even page checking can be problematic due to TeX's asynchronous output. For best results use the \checkoddpage and\ifoddpage macros from the changepage package called with the strict option. Simplistically:

\documentclass{...}
\usepackage[strict]{changepage}
\newcommand*{\oddeven}{\checkoddpage\ifoddpage ODD page \else EVEN page \fi}
...
\begin{document}
...
\oddeven

...
\end{document}
Peter Wilson
  • 28,066
  • I found a problem, for example, I replace the text of ODD page to \par \rule{0.5\linewidth}{4cm}\par, when I use many times \oddeven in one odd page, if the current page can't contain it at page bottom, it will be float to the new page (even page), that is say, even page have odd page object, do you have any idea for it. – zongxian Jun 15 '18 at 13:36
  • @zongxian No I don't. You are checking for an odd page and if so then add something to it but in your case there is no space for what you are adding. You might need something like the needspace package. – Peter Wilson Jun 15 '18 at 18:38
1

You can try this.

I define a counter to ensure the uniqueness of the labels.

\documentclass[]{article}

\usepackage{colortbl}
\usepackage[table]{xcolor}

\newcounter{mytabular}
\newcommand{\showtab}{\begin{flushleft}
        \stepcounter{mytabular}
        \ifodd\pageref{tabular:\themytabular}
            \begin{minipage}[t]{0.4\linewidth}
                \label{tabular:\themytabular}
                \begin{tabular}[t]{|p{\linewidth}|}
                    \hline
                    text text text text text\\
                    \hline
                \end{tabular}
            \end{minipage}\hfill
            \begin{minipage}[t]{0.5\linewidth}
                \begin{tabular}[t]{@{}p{\linewidth}@{}}
                    \hline
                    text text text text text text text text text text text text text text text text\\
                    \hline
                    text text text text text text text text\\
                    \hline
                    text text text text text text text text\\
                    \hline
                \end{tabular}
            \end{minipage}
        \else
            \begin{minipage}[t]{0.5\linewidth}
                \label{tabular:\themytabular}
                \begin{tabular}[t]{@{}p{\linewidth}@{}}
                    \hline
                    text text text text text text text text text text text text text text text text\\
                    \hline
                    text text text text text text text text\\
                    \hline
                    text text text text text text text text\\
                    \hline
                \end{tabular}
            \end{minipage}\hfill
            \begin{minipage}[t]{0.4\linewidth}
            \begin{tabular}[t]{|p{\linewidth}|}
                \hline
                text text text text text\\
                \hline
            \end{tabular}
        \end{minipage}
        \fi
\end{flushleft}}


\begin{document}
    \showtab
    \newpage

    \showtab
    \newpage

\end{document}
NBur
  • 4,326
  • 10
  • 27
  • Even/odd page checking can be problematic because of TeX's asynchronous output. For best results use the \ifoddpage macro from the changepage package with the strict option. – Peter Wilson Jun 14 '18 at 18:54