Draw rectangular frame in landscape page (tikz) latex

tikz-pgf

I have two document pages, one portrait, one landscape, on the same latex file. I need to draw a rectangular frame with 3cm left margin, 2cm right margin, 2cm top margin, 2cm bottom margin for both pages. For portrait page, I use the same code as below but the landscape page can't be drawn properly. I think that the co-ordinate is changed.

\documentclass{article}
\usepackage{tikz}
 \usepackage{pdflscape}
\usetikzlibrary{calc}
\usepackage[fontsize=14pt]{scrextend}%font mặc định
\usepackage[paperheight=21cm,paperwidth=29.7cm,right=2.6cm,left=3.5cm,top=2cm,bottom=2cm]{geometry}%font a4
\begin{document}

 \begin{tikzpicture}[overlay,remember picture]
\draw [line width=3pt]
   ($ (current page.north west)+ (3.0cm,-2.0cm) $)
    rectangle
   ($ (current page.south east) + (-2.0cm, 2.5cm) $);
\draw [line width=0.5pt]
   ($ (current page.north west) + (3.1cm,-2.1cm) $)
    rectangle
   ($ (current page.south east) + (-2.1cm,2.6cm) $);
\end{tikzpicture}

\newpage

\begin{landscape}
 \begin{tikzpicture}[overlay,remember picture]
\draw [line width=3pt]
   ($ (current page.north west)+ (3.0cm,-2.0cm) $)
    rectangle
   ($ (current page.south east) + (-2.0cm, 2.5cm) $);
\draw [line width=0.5pt]
   ($ (current page.north west) + (3.1cm,-2.1cm) $)
    rectangle
   ($ (current page.south east) + (-2.1cm,2.6cm) $);
\end{tikzpicture}
\end{landscape}
\end{document}

Best Answer

The pdflscape package has trouble with coordinates in landscape, but the typearea package works fine. Note the two lines that must be added when you switch orientation:

\KOMAoptions{paper=portrait,pagesize}
\recalctypearea

See these two answers for more information.

enter image description here

\documentclass{article}
\usepackage{tikz}
 %\usepackage{pdflscape}
 \usepackage[paper=portrait,pagesize]{typearea}
\usetikzlibrary{calc}
\usepackage[fontsize=14pt]{scrextend}%font mặc định
\usepackage[paperheight=21cm,paperwidth=29.7cm,right=2.6cm,left=3.5cm,top=2cm,bottom=2cm]{geometry}%font a4
\begin{document}

 \begin{tikzpicture}[overlay,remember picture]
\draw [line width=3pt]
   ($ (current page.north west)+ (3.0cm,-2.0cm) $)
    rectangle
   ($ (current page.south east) + (-2.0cm, 2.5cm) $);
\draw [line width=0.5pt]
   ($ (current page.north west) + (3.1cm,-2.1cm) $)
    rectangle
   ($ (current page.south east) + (-2.1cm,2.6cm) $);
\end{tikzpicture}

\newpage
\KOMAoptions{paper=portrait,pagesize}
\recalctypearea

 \begin{tikzpicture}[overlay,remember picture]
\draw [line width=3pt]
   ($ (current page.north west)+ (3.0cm,-2.0cm) $)
    rectangle
   ($ (current page.south east) + (-2.0cm, 2.5cm) $);
\draw [line width=0.5pt]
   ($ (current page.north west) + (3.1cm,-2.1cm) $)
    rectangle
   ($ (current page.south east) + (-2.1cm,2.6cm) $);
\end{tikzpicture}
\end{document}