使用 tikz 将文本拆分为多个节点

使用 tikz 将文本拆分为多个节点

我正在创建一个纸牌游戏,我想通过 TeX 生成纸牌。以下是代码的最小版本:

\documentclass[a4paper, landscape, oneside]{article}


%===========================================================================
% Libs
%===========================================================================

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}

\usepackage{graphicx}
\usepackage{xcolor}

\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{babel}

\usepackage{varwidth}

\usepackage{rotating}
\usepackage{lipsum}
\pagestyle{empty}

%===========================================================================
% Definitions
%===========================================================================
\def\ShapeCardFront{(0.0,0.0) rectangle (6.0,9.7)}
\def\ShapeCardBack{(6.0,0.0) rectangle (12.0,9.7)}
\def\ShapeFrontDescriptionII{(0.2,1.0) rectangle (5.8,7.0)}
\def\ShapeBackDescription{(6.1,0.1) rectangle (11.9,9.6)}

\tikzset{
    CardCorners/.style={
        rounded corners=0.2cm
    },
    ElementCorners/.style={
        rounded corners=0.1cm
    },
}

\newcommand{\CardFrontBorder}{
    \draw[black,CardCorners] \ShapeCardFront;
}

\newcommand{\CardBackBorder}{
    \draw[black,CardCorners] \ShapeCardBack;
}


\newcommand{\CardTypeII}[2]{
    \begin{scope}
        \clip[CardCorners] \ShapeCardFront;
        \fill[ElementCorners, white, opacity=0.95] \ShapeFrontDescriptionII;
        \draw[ElementCorners, color=#1, line width=2] \ShapeFrontDescriptionII;
    \end{scope}
    \node[right] at (0.1,0.5) {\textcolor{white}{\uppercase{\textbf{#2}}}};
    \fill[CardCorners, color=#1] \ShapeCardBack;
    \fill[CardCorners, white] \ShapeBackDescription;
}

\newcommand{\CardContent}[1]{
    \node[below] at (3.0,7.0) {
    \begin{varwidth}{5.4cm}
      \small #1
    \end{varwidth}
    };
}

\newcommand{\CardBackContent}[1]{
    \node[below] at (9.0,9.55) {
    \begin{varwidth}{5.6cm}
      \small #1
    \end{varwidth}
    };
}

\newcommand{\CardTypeCharacter}{\CardTypeII{characterbg}{Monster}}
\definecolor{characterbg}{RGB}{200,050,050}


%===========================================================================
% Document
%===========================================================================

\begin{document}

\begin{tikzpicture}
\CardTypeCharacter
\CardContent{\Huge Block A \\ \scriptsize \lipsum[1]}
\CardBackContent{\Huge Block B}
\CardFrontBorder
\CardBackBorder
\end{tikzpicture}

\end{document}

以下是我得到的结果:

在此处输入图片描述

现在我想知道是否可以将文本拆分到两个红色框中(我分别将其称为块 A 和块 B)。特别是当文本超出块 A 时,我想填充块 B(无需手动执行)。

提前感谢你的帮助

答案1

magazinetcolorbox可以帮助您。此库可用于将长文本拆分为固定长度的片段。这些片段存储在数组中,可以在需要时以不同的顺序打印。

在这个特殊情况下,我们需要CardContents使用magazine-tcolorbox已知大小的 来构建(第一个片段的宽度为 5.4 厘米,高度为 5.8 厘米)。当卡片内容的长度超过 5.8 厘米时,它将分成两个片段,第一个片段用于\CardFrontContent,第二个片段用于\CardBackContent

\documentclass[a4paper, landscape, oneside]{article}


%===========================================================================
% Libs
%===========================================================================

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{lmodern}

%\usepackage{graphicx}  %<-- TikZ already loads graphicx and xcolor
%\usepackage{xcolor}

\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{babel}

%\usepackage{varwidth}

%\usepackage{rotating}
\usepackage{lipsum}
\pagestyle{empty}

\usepackage[most]{tcolorbox}

\newtcolorbox{CardContent}{enhanced jigsaw, width=5.4cm, blankest, 
breakable, break at=5.8cm, reset box array, store to box array}

%===========================================================================
% Definitions
%===========================================================================
\def\ShapeCardFront{(0.0,0.0) rectangle (6.0,9.7)}
\def\ShapeCardBack{(6.0,0.0) rectangle (12.0,9.7)}
\def\ShapeFrontDescriptionII{(0.2,1.0) rectangle (5.8,7.0)}
\def\ShapeBackDescription{(6.1,0.1) rectangle (11.9,9.6)}

\tikzset{
    CardCorners/.style={
        rounded corners=0.2cm
    },
    ElementCorners/.style={
        rounded corners=0.1cm
    },
}

\newcommand{\CardFrontBorder}{
    \draw[black,CardCorners] \ShapeCardFront;
}

\newcommand{\CardBackBorder}{
    \draw[black,CardCorners] \ShapeCardBack;
}


\newcommand{\CardTypeII}[2]{
    \begin{scope}
        \clip[CardCorners] \ShapeCardFront;
        \fill[ElementCorners, white, opacity=0.95] \ShapeFrontDescriptionII;
        \draw[ElementCorners, color=#1, line width=2] \ShapeFrontDescriptionII;
    \end{scope}
    \node[right] at (0.1,0.5) {\textcolor{white}{\uppercase{\textbf{#2}}}};
    \fill[CardCorners, color=#1] \ShapeCardBack;
    \fill[CardCorners, white] \ShapeBackDescription;
}

%\newcommand{\CardContent}[1]{
%    \node[below] at (3.0,7.0) {
%    \begin{varwidth}{5.4cm}
%      \small #1
%    \end{varwidth}
%    };
%}
%
%\newcommand{\CardBackContent}[1]{
%    \node[below] at (9.0,9.55) {
%    \begin{varwidth}{5.6cm}
%      \small #1
%    \end{varwidth}
%    };
%}

\newcommand{\CardFrontContent}{
    \node[below] at (3.0,7.0) {
    \useboxarray{1}
    };
}

\newcommand{\CardBackContent}{
    \node[below] at (9.0,9.55) {
    \useboxarray{2}
    };
}

\newcommand{\CardTypeCharacter}{\CardTypeII{characterbg}{Monster}}
\definecolor{characterbg}{RGB}{200,050,050}


%===========================================================================
% Document
%===========================================================================

\begin{document}

\begin{CardContent}
\Huge Block A \\ \scriptsize \lipsum[1]\end{CardContent}

\begin{tikzpicture}
\CardTypeCharacter
\CardFrontContent
\CardBackContent
\CardFrontBorder
\CardBackBorder
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

看来您已经得到了符合您需求的答案,但我仍想补充一些想法和建议。如果这不是您想要的解决方案,我深表歉意。

写这个答案的重点是建议你把卡片的正面和背面放入普通单独的页面

我不知道如何在 LaTeX 中轻松实现这一点。也许,可以使用一些简单的文档类,例如standalonegeometry包来设置正确的尺寸,最后tikz设置覆盖层。较大的文本可能是标题。

从你的问题中我也不清楚你是否需要非常灵活的解决方案来处理其他卡。无论如何,我尝试使用 ConTeXt 制作一些东西,我更了解如何配置它(但不是最佳的,所以可以考虑简化)。结果如下(请注意,这是并排的两个页面的屏幕截图)

带有 ConTeXt 的卡片

生成上述文档的代码如下(我没有仔细查看您的代码,因此我使用的尺寸与您的不同):

% Define and setup the card size
\definepapersize[card][width=160bp,height=240bp]
\setuppapersize[card]

% Define the layout for odd (=left in the picture) pages
\definelayout[odd][
backspace=16bp,
margin=0bp,
edge=0bp,
topspace=80bp,
top=32bp,
topdistance=32bp,
width=128bp,
header=24bp,
headerdistance=8bp,
footer=0bp,
bottom=0bp,
height=fit,
]

% Define the layout for even (=right in the picture) pages
\definelayout[even][
backspace=16bp,
margin=0bp,
edge=0bp,
topspace=16bp,
width=128bp,
header=24bp,
headerdistance=8bp,
footer=0bp,
bottom=0bp,
bottomspace=0bp,
height=fit,
]

% This is needed to 
% 1) make the document doublesided and 
% 2) prevent the page number from showing
\setuppagenumbering[alternative=doublesided,state=stop]

% Set the style of the header
\setupheader[style={\definedfont[Serif at 20bp]}]
% Set the content of the header
\setupheadertexts[\doifrightpageelse{Block A}{Block B}]

% Set some font parameters. They are not
% really necessary, but anyways
\setuptolerance[stretch]
\setupalign[hz,hanging,line,nothyphenated]
\definefontfeature[default][default][protrusion=quality,expansion=quality,onum=yes]
\definebodyfontenvironment[6bp][interlinespace=8bp]
\setupbodyfont[termes,6pt]

% Define the frame for the odd page
\startuseMPgraphic{cardframeodd}
path p,q;
p = unitsquare xyscaled(OverlayWidth,OverlayHeight) cornered 0.4cm;
draw p withpen pencircle scaled 1bp;
q = unitsquare xyscaled(OverlayWidth-16bp,OverlayHeight-72bp) shifted (8bp,8bp) cornered 0.4cm;
draw q withpen pencircle scaled 2bp withcolor darkred;
clip currentpicture to p;
\stopuseMPgraphic

% Put the MetaPost in an overlay
\defineoverlay[cardframeodd][\useMPgraphic{cardframeodd}]

% Set the overlay on "rightpage" (which is odd page, shown to the left in the picture)
\setupbackgrounds[rightpage][paper][background=cardframeodd]

% Below the frame for the even page is setup similarly
\startuseMPgraphic{cardframeeven}
path p;
p = unitsquare xyscaled(OverlayWidth,OverlayHeight) cornered 0.4cm;
draw p withpen pencircle scaled 4bp withcolor darkred;
draw p withpen pencircle scaled 1bp;
clip currentpicture to p;
\stopuseMPgraphic
\defineoverlay[cardframeeven][\useMPgraphic{cardframeeven}]
\setupbackgrounds[leftpage][paper][background=cardframeeven]


\starttext
% Just put some text into the document
\dorecurse{4}{\input ward\par}
\stoptext

相关内容