Comment dessiner une ligne verticale avec exactement la hauteur d'un parbox à côté

Dec 04 2020

J'essaie de créer de petites sections pour un cv où le nom de la section et une ligne verticale devraient être à côté du contenu. Le nom de la section doit être centré et la ligne verticale doit correspondre exactement à la hauteur du contenu. Voici la sortie souhaitée:

Maintenant, avec mon code, je ne sais pas comment définir la hauteur de la ligne verticale pour obtenir exactement la hauteur du \parbox. C'est pourquoi pour la démonstration, je l'ai réglé à 1 cm. Est-ce que quelqu'un sait comment obtenir la sortie souhaitée avec de petites modifications de mon code ou une meilleure façon d'y parvenir?

\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}

J'ai également essayé d'accéder à la hauteur de parbox, qui ne fonctionnait pas pour moi: Hauteur d'accès d'un \ parbox

Merci d'avance.

Ma sortie:

Réponses

4 RuixiZhang Dec 04 2020 at 20:57

Que diriez-vous du code suivant? La documentation est fournie dans le code sous forme de commentaires.

\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}