如何使用 TikZ 将一个文本块与一对垂直线绑定在一起?

如何使用 TikZ 将一个文本块与一对垂直线绑定在一起?

梅威瑟:

\begin{center}
\begin{tikzpicture}[draw=DarkBlue,line width=1.0pt,inner sep=12pt,inner ysep=6pt]
\node at (0,0) [rectangle,draw,fill=LightBlue]{ % 
\begin{minipage}[t]{118mm} %

A wide block of text contained in a complete rectangle.

I want only the left and right lines to appear.

i. e. open at the top and the bottom, but keep the shading.\vspace{3pt}

Tried adapting the line commented-out below, but cannot control placement.\vspace{2pt}
% \draw [line width=0.25mm, red ] (0,-1) -- (2,-1) node [right] {\SI{0.25}{\milli\meter}};;
\end{minipage}};
\end{tikzpicture}
\end{center}

答案1

您可以命名节点并分别在角之间画线:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[draw=blue!60!black,line width=1.0pt,inner sep=12pt,inner ysep=6pt]
\node[rectangle, outer sep=0pt, fill=blue!10!white] (text node) at (0,0) { %
\begin{minipage}[t]{118mm} %

A wide block of text contained in a complete rectangle.

I want only the left and right lines to appear.

i. e. open at the top and the bottom, but keep the shading.\vspace{3pt}

Tried adapting the line commented-out below, but cannot control placement.\vspace{2pt}
% \draw [line width=0.25mm, red ] (0,-1) -- (2,-1) node [right] {\SI{0.25}{\milli\meter}};;
\end{minipage}};
\draw (text node.south west) -- (text node.north west) (text node.south east) -- (text node.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我知道楼主想要一个TiKZ解决方案,但也许他不知道tcolorbox其主要目标是制作彩色的文本框。

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}

\newtcolorbox{mybox}[2][]{%
notitle,
colback=blue!10!white,
colframe=blue!60!black,
sharp corners,
boxsep=0pt,
left=12pt, right=12pt,
top=6pt, bottom=6pt,
boxrule=0pt,leftrule=1pt,rightrule=1pt,
width=#2,
#1
}

\begin{document}
\begin{mybox}{118mm}
A wide block of text contained in a complete rectangle.

I want only the left and right lines to appear.

i. e. open at the top and the bottom, but keep the shading.

Tried adapting the line commented-out below, but cannot control placement.
\end{mybox}

\begin{mybox}[colback=red!30]{60mm}
A wide block of text contained in a complete rectangle.

I want only the left and right lines to appear.

i. e. open at the top and the bottom, but keep the shading.

Tried adapting the line commented-out below, but cannot control placement.
\end{mybox}
\end{document}

在此处输入图片描述

相关内容