在 TikZ 中解决 parboxes 的宽度和位置问题

在 TikZ 中解决 parboxes 的宽度和位置问题

我正在尝试用 LaTeX 更新自己的简历。我已经好几年没排版了,所以我的 LaTeX 已经非常生疏了。我看了几门我喜欢的“cv”课程(limecv、fortysecondscv)以寻求灵感,现在正在学习 TikZ。

我的问题是,当我尝试将文本元素放置在设计元素上时,所有 parbox 都变得太宽,大约 0.5 厘米。此外,它们并没有准确地放置在我期望的位置,而是大约 1 毫米远。我需要将某种“whatever-sep(s)”设置为 0 吗,或者我遗漏了什么?

下面是一个 MWE,显示了方框和我想要的尺寸,背景中有网格。如果你数一下方格,你会发现所有的方框都有点太宽了。

\documentclass{article}

\usepackage{calc}

% Define margin size
\newlength{\cvMargin}
\setlength{\cvMargin}{1.25cm}

% Set papersize and basic layout
\usepackage[
    a4paper,
    noheadfoot,
    nomarginpar,
    margin=\cvMargin,
    showframe
    ]{geometry}

% Define width of main and side columns
\newlength{\cvSideWidth}
\setlength{\cvSideWidth}{0.33333\paperwidth}
\newlength{\cvSideTextWidth}
\setlength{\cvSideTextWidth}{\cvSideWidth-2\cvMargin}
\newlength{\cvMainWidth}
\setlength{\cvMainWidth}{\paperwidth-\cvSideWidth}
\newlength{\cvMainTextWidth}
\setlength{\cvMainTextWidth}{\cvMainWidth-\cvMargin}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\pagestyle{empty}
\setlength{\parindent}{0pt}

% \convertto{mm}{1pt}
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\usepackage{lipsum}

\begin{document}

\begin{tikzpicture}[
  remember picture, overlay,
  shift={(current page.north west)} % Needed for correct help grid origin
  ] 

  \draw[help lines]
      (0,0) grid ++(\paperwidth,-\paperheight);
   
  \node[left] at (current page.east)
      {\fbox{%
          \parbox{\paperwidth}
          {\centering{}paperwidth: \convertto{cm}{\the\paperwidth}\ cm}
        }
      };

  \node[left,yshift=-5ex] at (current page.east)
      {\fbox{%
          \parbox{\cvSideWidth}
          {\centering{}cvSideWidth: \convertto{cm}{\the\cvSideWidth}\ cm\hfill}
        }
      };   

  \node[left,xshift=-\cvMargin,yshift=-10ex] at (current page.east)
      {\fbox{%
          \parbox{\cvSideTextWidth}
          {\centering{}cvSideTextWidth: \convertto{cm}{\the\cvSideTextWidth}\ cm\hfill}
        }
      };
      
  \node[right,yshift=-15ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainWidth}
          {\centering{}cvMainWidth: \convertto{cm}{\the\cvMainWidth}\ cm\hfill}
        }
      };
      
  \node[right,xshift=\cvMargin,yshift=-20ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainTextWidth}
          {\centering{}cvMainTextWidth: \convertto{cm}{\the\cvMainTextWidth}\ cm\hfill}
        }
      };        
      
  \node[right,xshift=\cvMargin,yshift=-45ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainTextWidth}
          {\lipsum[2-3]}
        }
      };
      
\end{tikzpicture}

\end{document}

答案1

每个node都有一个inner sep不为零的默认值。此分隔是其内容和边框之间的空间。

您没有draw节点,因此您看不到它们的边界,但可以看到它们的边界fbox。如果您想fbox调整它们的位置,请inner {x|y}sep=0pt在节点中使用。

在下面的代码中我添加了一些红色节点来查看效果。

\documentclass{文章}

\usepackage{calc}

% Define margin size
\newlength{\cvMargin}
\setlength{\cvMargin}{1.25cm}

% Set papersize and basic layout
\usepackage[
    a4paper,
    noheadfoot,
    nomarginpar,
    margin=\cvMargin,
    showframe
    ]{geometry}

% Define width of main and side columns
\newlength{\cvSideWidth}
\setlength{\cvSideWidth}{0.33333\paperwidth}
\newlength{\cvSideTextWidth}
\setlength{\cvSideTextWidth}{\cvSideWidth-2\cvMargin}
\newlength{\cvMainWidth}
\setlength{\cvMainWidth}{\paperwidth-\cvSideWidth}
\newlength{\cvMainTextWidth}
\setlength{\cvMainTextWidth}{\cvMainWidth-\cvMargin}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\pagestyle{empty}
\setlength{\parindent}{0pt}

% \convertto{mm}{1pt}
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\usepackage{lipsum}

\begin{document}

\begin{tikzpicture}[
  remember picture, overlay,
  shift={(current page.north west)} % Needed for correct help grid origin
  ] 

  \draw[help lines]
      (0,0) grid ++(\paperwidth,-\paperheight);
   
  \node[left] at (current page.east)
      {\fbox{%
          \parbox{\paperwidth}
          {\centering{}paperwidth: \convertto{cm}{\the\paperwidth}\ cm}
        }
      };

  \node[left, draw=red, inner sep=0pt] at ([yshift=8mm]current page.east)
      {\parbox{\paperwidth}
          {\centering{}paperwidth: \convertto{cm}{\the\paperwidth}\ cm}
      };

  \node[left,yshift=-5ex] at (current page.east)
      {\fbox{%
          \parbox{\cvSideWidth}
          {\centering{}cvSideWidth: \convertto{cm}{\the\cvSideWidth}\ cm\hfill}
        }
      };   

  \node[left,yshift=-5ex, draw=red, inner sep=0pt] at (current page.east)
      {\parbox{\cvSideWidth}
          {\centering{}cvSideWidth: \convertto{cm}{\the\cvSideWidth}\ cm\hfill}
      };   

  \node[left,xshift=-\cvMargin,yshift=-10ex] at (current page.east)
      {\fbox{%
          \parbox{\cvSideTextWidth}
          {\centering{}cvSideTextWidth: \convertto{cm}{\the\cvSideTextWidth}\ cm\hfill}
        }
      };

  \node[left,xshift=-\cvMargin,yshift=-15ex, draw=red, inner xsep=0pt] at (current page.east)
      {\fbox{\parbox{\cvSideTextWidth}
          {\centering{}cvSideTextWidth: \convertto{cm}{\the\cvSideTextWidth}\ cm\hfill}}
      };
      
  \node[right,yshift=-15ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainWidth}
          {\centering{}cvMainWidth: \convertto{cm}{\the\cvMainWidth}\ cm\hfill}
        }
      };
      
  \node[right,xshift=\cvMargin,yshift=-20ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainTextWidth}
          {\centering{}cvMainTextWidth: \convertto{cm}{\the\cvMainTextWidth}\ cm\hfill}
        }
      };        
      
  \node[right,xshift=\cvMargin,yshift=-45ex] at (current page.west)
      {\fbox{%
          \parbox{\cvMainTextWidth}
          {\lipsum[2-3]}
        }
      };
      
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容