在区域外绘制 tikz 图形

在区域外绘制 tikz 图形

我想将带有编号和章节的黑色框放置在文本行的外面。我想将其放置在右侧 1 厘米处。

\documentclass[oneside,10pt,oldfontcommands,standalone]{memoir}   
\usepackage[T1]{fontenc}

\usepackage{amsmath}
\usepackage{fix-cm}    


\makeatletter
\newcommand\HUGEr{\@setfontsize\Huge{50}{60}} %used to set the number in chapter section
\makeatother    

\usepackage{blindtext}

\usepackage{tikz}
\makechapterstyle{box}{
    \renewcommand*{\printchaptername}{}
    \renewcommand*{\chapnumfont}{\normalfont\sffamily\HUGEr\bfseries}
    \renewcommand*{\printchapternum}{
        \flushright
        \begin{tikzpicture}
        \draw[fill,color=black] (0,0) rectangle (2cm,2cm) node[color=white]  (test) {};
        \draw[color=white] (1cm,1cm) node { \chapnumfont\thechapter };
        \draw[color=white] (-0.5,0) rectangle (-0.1,2) node[midway,rotate = 90,color=black] () {Chapter};
        \end{tikzpicture}
    }
    \renewcommand*{\chaptitlefont}{\normalfont\sffamily\HUGE\bfseries}
    \renewcommand*{\printchaptertitle}[1]{\flushright\chaptitlefont##1}
}
\chapterstyle{box}

\begin{document}
\chapter{Introduction}  
\Blindtext
\end{document}

图解

答案1

\hspace*{1cm}环境内部的tikzpicture工作。或者,您也可以重新定义边界框tikzpicture(这在其他场景中也很有用 - 请参阅第 175 页及后续内容)。建立边界框PGF 手册)。附注:tikzpicture如果可以的话,我一般不建议在 for 布局中混合使用相对长度和绝对长度。如果将所有内容都保留为相对长度(所以:(2,-2)而不是(2cm,-2cm)),它将使您的代码更加灵活,而且输入的内容更少(当然,有时这是无法避免的,这只是我多年来的个人偏好)。

结果:

结果

\documentclass[oneside,10pt,oldfontcommands,standalone]{memoir}   
\usepackage[T1]{fontenc}

\usepackage{amsmath}
\usepackage{fix-cm}    


\makeatletter
\newcommand\HUGEr{\@setfontsize\Huge{50}{60}} %used to set the number in chapter section
\makeatother    

\usepackage{blindtext}

\usepackage{tikz}
\makechapterstyle{box}{
    \renewcommand*{\printchaptername}{}
    \renewcommand*{\chapnumfont}{\normalfont\sffamily\HUGEr\bfseries}
    \renewcommand*{\printchapternum}{%
        \flushright
        \begin{tikzpicture}
            % Either version works -- use whichever is preferred or comes with fewer undesirable
            % side effects for the particular scenario.
            %\hspace*{1cm}
            \path[use as bounding box] (-1,0) rectangle (1,2); %will scale with tikzpicture units

            \draw[fill,color=black] (0,0) rectangle (2,2) node[color=white]  (test) {};
            \draw[color=white] (1,1) node { \chapnumfont\thechapter };
            \draw[color=white] (-0.5,0) rectangle (-0.1,2) node[midway,rotate = 90,color=black] () {Chapter};
        \end{tikzpicture}
    }
    \renewcommand*{\chaptitlefont}{\normalfont\sffamily\HUGE\bfseries}
    \renewcommand*{\printchaptertitle}[1]{\flushright\chaptitlefont##1}
}
\chapterstyle{box}

\begin{document}
\chapter{Introduction}  
\Blindtext
\end{document}

相关内容