我正在尝试使用 TikZ 制作一个新的类似部分的标题。我基本上希望它看起来像这样(别介意时髦的背景):
但我只能得到这样的结果:
在我的 LaTeX 文档中我这样调用:
\problems
下面是我用来产生上述(不令人满意的)结果的代码:
\RequirePackage{tikz}
\makeatletter
\pgfdeclareshape{topbot}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{north west}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{base}
\backgroundpath{%
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}} %left hand vertical line
\pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yb}} %top vertical line
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
}%
}
\makeatother
\newcommand{\problems}{
\begin{tikzpicture}
\node[draw,shape=topbot,very thick, minimum width=\paperwidth, text width=.8\paperwidth]
{\large\textbf{Problems for Section~\thesection}};
\end{tikzpicture}}
\titlespacing*{\problems}{-0.25in}{50pt}{0pt}
我试图制作矩形的左下角,然后使左侧变得非常厚。
另外,我希望矩形的宽度与页面宽度相同。(这不会被打印出来,因此我不太关心边距。)
答案1
这是一个不使用 TikZ 的解决方案,使用标题安全包装(我不确定水平线及其悬垂的长度,但您可以轻松更改尺寸):
\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\newcommand\problems{
\begingroup
\titleformat{\chapter}[display]
{\normalfont\large\filright\bfseries}{\Huge}{0pt}
{\hspace*{-1.7cm}##1\vskip-6ex}[\hspace*{-3cm}\rlap{\rule{4ex}{4ex}\rule{\paperwidth}{2pt}}]
\titlespacing*{\chapter}{0pt}{50pt}{20pt}
\chapter*{Problems for section~\thesection}
\endgroup
}
\begin{document}
\setcounter{chapter}{1}\setcounter{section}{1}% just for the example
\problems
\lipsum[1]
\end{document}
答案2
我认为,您可以在 TiKZ 中通过使用简单的\fill
命令和使用相对坐标来最好地完成此操作,如下所示:
来源:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
% Various lengths, vary to taste
\newlength\tablength \setlength\tablength{20pt}
\newlength\ruledepth \setlength\ruledepth{2pt}
\newlength\rulelength \setlength\rulelength{0.95\textwidth}
\newlength\tabheight \setlength\tabheight{0.25in} \addtolength\tabheight{-\ruledepth}
\newlength\raisetitle \setlength\raisetitle{1ex}
\newlength\insettitle \setlength\insettitle{1.5em}
\newlength\afterheadingskip \setlength\afterheadingskip{2ex}
\newcommand\problems{%
\par\noindent
\begin{tikzpicture}
\node [inner sep=0pt] (title) at (0,0) {\bfseries Problems for Section~\thesection};
\coordinate (inner corner) at ($(title.south west) + (-\insettitle,-\raisetitle)$);
\coordinate (outer corner) at ($(inner corner) + (-\tablength,-\ruledepth)$);
\fill [black] (outer corner) -- +(\rulelength,0) -- +(\rulelength,\ruledepth)
-- (inner corner) -- +(0,\tabheight) -- +(-\tablength,\tabheight)
-- cycle;
\end{tikzpicture}%
\\[\afterheadingskip]}
\begin{document}
\problems Text immediately following the section heading.
\end{document}
结果: