如何绘制与旁边的 parbox 高度完全相同的垂直线

如何绘制与旁边的 parbox 高度完全相同的垂直线

我正在尝试为简历创建小部分,其中部分名称和垂直线应位于内容旁边。部分名称应居中,垂直线应正好是内容的高度。这是所需的输出:

在此处输入图片描述

现在,使用我的代码,我不知道如何设置垂直线高度以精确获得的高度\parbox。这就是为什么为了演示,我将其设置为 1cm。有人知道如何通过对我的代码进行小幅修改来获得我想要的输出,或者有更好的方法来实现这一点吗?

\documentclass[12pt]{standalone}

\usepackage{tikz}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.}

\newcommand{\customsection}[2]{
    \begin{minipage}{0.05\textwidth}
        \rotatebox[origin=c]{90}{#1}
        \hspace{0.01cm}
        \begin{tikzpicture}
            \fill [bottom color=red, top color=blue] (0cm, 0cm) rectangle (-0.05cm, 1cm);
        \end{tikzpicture}
    \end{minipage}
    \parbox[c]{0.85\textwidth}{
        #2
    }
}


\begin{document}
    \customsection{TEST}{\lorem \\ \lorem \\ \lorem}
\end{document}

我也尝试过访问 parbox 高度,但这对我来说不起作用: 访问 \parbox 的高度

提前致谢。

我的输出: 在此处输入图片描述

答案1

下面的代码怎么样?代码中以注释的形式提供了文档。

\documentclass[12pt]{standalone}

\usepackage{tikz}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.}

% Define owmal's box to store the \parbox
\newsavebox\owmalsbox

% Notice where `%' are added to suppress end-of-line spaces!
\newcommand{\customsection}[2]{%
% First we save \parbox into \owmalsbox.
% We also use \strut both at the beginning and at the end,
% and this will ensure that the first line and the last line
% have the same height (in most cases).
    \sbox\owmalsbox{%
        \parbox[c]{0.85\textwidth}{%
            \strut#2\strut
        }%
    }%
% The total height of the \parbox is now available as
% \the\ht\owmalsbox+\the\dp\owmalsbox, so we can put our
% side-way text at (\the\ht\owmalsbox+\the\dp\owmalsbox)/2,
% and draw our rectangle accordingly. The minipage has the
% optional argument `c' to go with `\parbox[c]...' above.
% Notice again the use of \strut in the side-way text.
    \begin{minipage}[c]{0.05\textwidth}
        \begin{tikzpicture}
            \node [rotate=90, above] at
                (-0.05cm, {(\the\ht\owmalsbox+\the\dp\owmalsbox)/2})
                    {\strut#1};
            \fill [bottom color=red, top color=blue]
                (0cm, 0cm)
                    rectangle
                (-0.05cm, \the\ht\owmalsbox+\the\dp\owmalsbox);
        \end{tikzpicture}%
    \end{minipage}%
% The following additional space depends on how you setup
% the minipage (it is 0.05\textwidth wide for now).
% Adjust these if necessary.
    \hspace{0.5cm}%
% Finally we typeset \owmalsbox by releasing its content
% (and also destroy its content).
    \unhbox\owmalsbox
}

\begin{document}
    \customsection{TEST}{\lorem \\ \lorem \\ \lorem}
\end{document}

定制部分

相关内容