Tikz:用文本填充节点

Tikz:用文本填充节点

这是一个最小的工作示例

%!TEX TS-program = xelatex
\documentclass{article}

%%%%%%%%%%
% Colors %
%%%%%%%%%%
\usepackage{xcolor}
\definecolor{green}{HTML}{C2E15F}
\definecolor{gray}{HTML}{4D4D4D}

%%%%%%%%%
% Fonts %
%%%%%%%%%
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setmainfont[Mapping=tex-text, Color=gray]{Helvetica Neue Light}

%%%%%%%%%%%%%%%%%
% Graphic tools %
%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}


%%%%%%%%%%
% Header %
%%%%%%%%%%
 \begin{tikzpicture} [remember picture,overlay]
   \node[](name)[align = left, inner sep = 0] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
   \fontsize{20}{5pt}\selectfont{My function}};
   \draw (name.north west) -- +(20cm, 0);
   \draw (name.south west) -- +(20cm, 0);
   \node[](about)[align=left,inner sep=0, right=5.0cm of name.north east, anchor=north west]{%
   \fontsize{12pt}{12pt}\selectfont{[email protected]} \\
   \fontsize{12pt}{12pt}\selectfont{+45 555 555} \\%
   \fontsize{12pt}{12pt}\selectfont{my adress} \\   
   \fontsize{12pt}{12pt}\selectfont{zipcode City} \\
   };
 \end{tikzpicture}

\end{document}

得到以下结果

latex1

我希望最右侧节点中的文本能够“填充”节点,即与我在图片上绘制的两条线完全对齐。 有可能吗? 也许可以使用迷你页面? 按照同样的想法,是否可以强制文本水平填充节点,以便文本左右对齐?

编辑:似乎可以通过使用节点选项来获得对齐的文本文本宽度最小宽度两端对齐的文本。但是,一旦在字符屏幕中输入“.”字符,这些选项似乎就不起作用,如以下两张图所示。

对齐文本1

对齐文本2

相关代码改编如下。

 \begin{tikzpicture} [remember picture,overlay]
   \node[](name)[align = left, inner sep = 0] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
   \fontsize{20}{5pt}\selectfont{My function}};
   \draw (name.north west) -- +(20cm, 0);
   \draw (name.south west) -- +(20cm, 0);
   \node[](about)[draw=black, thin, align=left,inner sep=0, right=5.0cm of name.north east, anchor=north west, text width=4cm, minimum width=4cm, text justified]{%
   \fontsize{12pt}{12pt}\selectfont{mymail@mailcom} 
   \fontsize{12pt}{12pt}\selectfont{+45 555 555} 
   \fontsize{12pt}{12pt}\selectfont{my adress}   
   \fontsize{12pt}{12pt}\selectfont{zipcode City} 
   };
 \end{tikzpicture}


 \begin{tikzpicture} [remember picture,overlay]
   \node[](name)[align = left, inner sep = 0] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
   \fontsize{20}{5pt}\selectfont{My function}};
   \draw (name.north west) -- +(20cm, 0);
   \draw (name.south west) -- +(20cm, 0);
   \node[](about)[draw=black, thin, align=left,inner sep=0, right=5.0cm of name.north east, anchor=north west, text width=4cm, minimum width=4cm, text justified]{%
   \fontsize{12pt}{12pt}\selectfont{[email protected]} 
   \fontsize{12pt}{12pt}\selectfont{+45 555 555} 
   \fontsize{12pt}{12pt}\selectfont{my adress}   
   \fontsize{12pt}{12pt}\selectfont{zipcode City} 
   };
 \end{tikzpicture}

答案1

借助以下工具测量高度时可以进行垂直填充\pgfgetlastxy

代码

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage[scaled]{helvet}
\renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif
\usepackage[T1]{fontenc}

\newcommand{\computeheight}[2]% name of node, name of targetmacro
{ \path (#1.south west);
  \pgfgetlastxy{\xsw}{\ysw}
  \path (#1.north east);
  \pgfgetlastxy{\xne}{\yne}
  %\pgfmathsetlengthmacro{\nodewidth}{\xne-\xsw}% not needed here
  \pgfmathsetlengthmacro{\nodeheight}{\yne-\ysw}
  %\xdef\widthofnode{\nodewidth}
  \expandafter\xdef\csname #2\endcsname{\nodeheight}
}

\begin{document}

\begin{tikzpicture} [remember picture,overlay]
  \node[right](name)[align = left, inner sep = 2pt] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
  \fontsize{20}{5pt}\selectfont{My function}};

    % get height of node "name"
  \computeheight{name}{nameheight}

  \draw (name.north west) -- +(18cm, 0);
  \draw (name.south west) -- +(18cm, 0);

    % helper coordinate
    \coordinate[right=5.0cm of name.north east, anchor=north west] (about);
    % the data you want to use; the {} is for protecting commas in the entries,
    % so it's not neccessary here
    \foreach \t [count=\c] in {{[email protected]},{+45 555 555},{my adress},{zipcode City}}
    {   \pgfmathsetlengthmacro{\topdistance}{(\c-1)*\nameheight/4+\nameheight/4*0.5}
        \node[below=\topdistance of about,align=left,anchor=west] (about\c) {\fontsize{12pt}{12pt}\selectfont{\t}};
    }
\end{tikzpicture}

\end{document}

输出

在此处输入图片描述

答案2

这是纯粹的 MS Word 类型,排版很糟糕,我希望你使用它有正当理由。我也希望你的地址能填满这一行。

对于没有换行符的文本,TikZ 有align=justify选项。你可以用空格代替换行符来移动内容。

 \begin{tikzpicture} [remember picture,overlay]
   \node[](name)[align = left, inner sep = 0] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
   \fontsize{20}{5pt}\selectfont{My function}};
   \draw (name.north west) -- +(20cm, 0);
   \draw (name.south west) -- +(20cm, 0);
   \node[](about)[inner sep=0, right=5.0cm of name.north east, anchor=north west,align=justify, text width=3.5cm]{%
   \fontsize{12pt}{12pt}\selectfont{[email protected]}
   \fontsize{12pt}{12pt}\selectfont{+45 555 555}\hspace{2em}
   \fontsize{12pt}{12pt}\selectfont{Wallie Street}\hspace{3em}
   \fontsize{12pt}{12pt}\selectfont{1234AB\hfill Lyon}
   };
 \end{tikzpicture}

在此处输入图片描述

— 我打电话询问我的工作面试的后续情况……

-毫无味道。别再给我们打电话了。

答案3

为了获得你想要的“填充”效果,你可以使用它

\fontsize{12pt}{12pt}\selectfont{[email protected]} \\[3pt]
\fontsize{12pt}{12pt}\selectfont{+45 555 555} \\[3pt]
\fontsize{12pt}{12pt}\selectfont{my adress} \\[3pt]   
\fontsize{12pt}{12pt}\selectfont{zipcode City}

在此处输入图片描述

答案4

通过循环内的少量计算即可实现垂直填充\foreach

%!TEX TS-program = xelatex
\documentclass{article}

%%%%%%%%%%
% Colors %
%%%%%%%%%%
\usepackage{xcolor}
\definecolor{green}{HTML}{C2E15F}
\definecolor{gray}{HTML}{4D4D4D}

%%%%%%%%%
% Fonts %
%%%%%%%%%
\usepackage{fontspec}
\defaultfontfeatures{Mapping=tex-text}
\setmainfont[Mapping=tex-text, Color=gray]{Arial}

%%%%%%%%%%%%%%%%%
% Graphic tools %
%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\begin{document}


%%%%%%%%%%
% Header %
%%%%%%%%%%
 \begin{tikzpicture} [remember picture,overlay]
   \node[](name)[align = left, inner sep = 0] {\fontsize{42pt}{0pt}\selectfont{My Name} \\
   \fontsize{20}{5pt}\selectfont{My function}};
   \draw (name.north west) -- +(20cm, 0);
   \draw (name.south west) -- +(20cm, 0);
%    \node[](about)[align=left,inner sep=0, right=5.0cm of name.north east, anchor=north west]{%
%    \fontsize{12pt}{12pt}\selectfont{[email protected]} \\
%    \fontsize{12pt}{12pt}\selectfont{+45 555 555} \\%
%    \fontsize{12pt}{12pt}\selectfont{my adress} \\   
%    \fontsize{12pt}{12pt}\selectfont{zipcode City} \\
%    };
   \node(abouttop)[align=left,inner sep=0, right=5.0cm of name.north east, anchor=north west]{[email protected]};
   \node(aboutbottom)[align=left,inner sep=0, right=5.0cm of name.south east, anchor=south west]{zipcode City};
   \newcommand{\nodelist}{{+45 555 555},{my adress}}
   \newcommand{\nodelistlength}{2} %try to calculate this automatically
   \foreach \nodetext [count=\nodenumber] in \nodelist
    {
    \node[align=left,inner sep=0,anchor=base west] at ($(abouttop.base west)!\nodenumber/(1+\nodelistlength)!(aboutbottom.base west)$){\nodetext};
    }
 \end{tikzpicture}

\end{document}

我不擅长列表计算LaTeX,这就是为什么它有点不合适。这个答案可以通过以下方式改进:

  1. 自动计算行列表的长度。
  2. 只需提交 4 行并删除第一行和最后一行,以便自动明确放置它。
  3. 使用tikz样式。

相关内容