Tikz 用于多语言章节标题

Tikz 用于多语言章节标题

当我添加 polyglossia(在代码中注释)时,代码不起作用。我不知道错误在哪里。有人能帮助我吗?

\documentclass[10pt,a4paper,openany]{book}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric,decorations.pathmorphing,decorations.text}
\usepackage{verbatim}
\usepackage{fontspec}
\usepackage{kpfonts}
\usepackage[explicit]{titlesec}
\makeatletter  
\newcommand*\chapterlabel{}
\titleformat{\chapter}
  {\gdef\chapterlabel{}
   \normalfont\sffamily\Huge\bfseries\scshape}
  {\gdef\chapterlabel{\thechapter\ }}{0pt}
  {\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
          (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
              rounded corners=20pt,inner sep=11pt,
              fill=MidnightBlue]
              {\color{white}\chapterlabel#1};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}

\makeatother  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{polyglossia}
%\setmainlanguage{arabic}
%\setotherlanguage{english}
%\setmainlanguage[numerals=maghrib]{arabic}
%\newfontfamily\arabicfont[Script=Arabic]{Amiri}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\tableofcontents
\chapter{Introduction}
Text
\chapter{Main}
\section{Section}
Text
\chapter{الثاني}
\chapter{three}
\begin{thebibliography}{99}
\bibitem{Test} test reference
\end{thebibliography}
\end{document}

答案1

这是一个有效但有局限性的解决方案。

当您使用\chapter{قائمة المحتويات}\chapter*{قائمة المحتويات}(在下面的示例中)时,TiZ 图像不会出现,这似乎是标题安全我认为包。唯一有效的方法是\chapter[قائمة المحتويات]{}强制使用可选参数。此外,没有这样的东西\chapter*[قائمة المحتويات]{}

您可以\section{...}照常使用,因为没有 TiZ 参与。

输出

% Run with XeLaTeX and the order of preamble is important!
\documentclass[10pt,a4paper,openany]{book}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric,decorations.pathmorphing,decorations.text}
\usepackage{verbatim}
\usepackage{fontspec}
\usepackage{kpfonts}
\usepackage[explicit]{titlesec}
\usepackage{nameref}                % Added! Needed to get chapter name.
\usepackage[toc,page]{appendix}     % Added! Completely optional.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{polyglossia}
\setmainlanguage[numerals=mashriq]{arabic}
\setotherlanguage{english}
\newfontfamily\arabicfont[Script=Arabic]{Times New Roman}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
% To get the chapter name in the [], i.e., \chapter[store this]{Something}
\newcommand{\currentname}{\@currentlabelname}
% Changes the toc title. You need to use this insead of changing the 
% definition of \contentsname.
\renewcommand\tableofcontents{%
    \chapter[قائمة المحتويات]{}
    \@starttoc{toc}%
}
\makeatother

% The format of the table of content page and bibliography.
\newcommand{\customTitleFormat}[1]{%
\titleformat{name=\chapter}
 {\normalfont\huge\bfseries}{
\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
            \draw[fill=LightSkyBlue] (0,0) rectangle
              (\paperwidth,3cm);
            \node[anchor=east,xshift=.9\paperwidth,rectangle, 
            rounded corners=20pt,inner sep=11pt,fill=MidnightBlue,
            text=white]{#1};
        \end{tikzpicture}
      };
   \end{tikzpicture}
}{20pt}{\Huge}
\titlespacing*{name=\chapter}{0pt}{50pt}{-60pt}
}

% The format of all chapters in the document.
\newcommand{\chapterformat}{%
 \titleformat{name=\chapter}
{\normalfont\huge\bfseries}{
\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
            \draw[fill=LightSkyBlue] (0,0) rectangle
            (\paperwidth,3cm);
            \node[anchor=east,xshift=.9\paperwidth,rectangle, 
            rounded corners=20pt,inner sep=11pt,fill=MidnightBlue,
            text=white]{\currentname\ \thechapter};
        \end{tikzpicture}
      };
   \end{tikzpicture}
}{20pt}{\Huge}
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}
}

\begin{document}
    \renewcommand{\bibname}{}%Clears the bibliography title
    \customTitleFormat{قائمة المحتويات}
    \tableofcontents
    %
    \chapterformat
    \chapter[المقدمة]{}
    Text
    \chapter[الفصل التالي]{}
    More text
    \section{البداية}
    Extra more text
    %
    \customTitleFormat{قائمة المراجع}
    \begin{thebibliography}{99}
        \bibitem{Test} test reference
    \end{thebibliography}
    %
    \begin{appendices}
        \chapter[الجزء الاول]{}
        Infomration...
    \end{appendices}
\end{document}

答案2

不要使用explicit,但要利用这样一个事实:章节标题作为括号参数传递给最后一个强制参数末尾的代码\titleformat

因此,我们让该参数以双参数命令结尾,其中第一个参数对于编号和未编号章节的指定不同。

我不确定嵌套tikzpicture,但是,它有效。

\documentclass[10pt,a4paper,openany]{book}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric,decorations.pathmorphing,decorations.text}
\usepackage{fontspec}
\usepackage{titlesec}
\usepackage{polyglossia}

\setmainlanguage{arabic}
\setotherlanguage{english}
\setmainlanguage[numerals=maghrib]{arabic}
\newfontfamily\arabicfont[Script=Arabic]{Amiri}
\newfontfamily\arabicfontsf[Script=Arabic]{Amiri}

\titleformat{name=\chapter}
  {\normalfont\sffamily\Huge\bfseries\scshape}
  {}
  {0pt}
  {\aaachapter{\thechapter\ }}
\titleformat{name=\chapter,numberless}
  {\normalfont\sffamily\Huge\bfseries\scshape}
  {}
  {0pt}
  {\aaachapter{}}

\newcommand{\aaachapter}[2]{%
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
      {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
          (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
              rounded corners=20pt,inner sep=11pt,
              fill=MidnightBlue]
              {\color{white}#1#2};
       \end{tikzpicture}
      };
   \end{tikzpicture}
  }
\titlespacing*{\chapter}{0pt}{50pt}{-60pt}


\begin{document}
\tableofcontents
\chapter{Introduction}
Text
\chapter{Main}
\section{Section}
Text
\chapter{الثاني}
\chapter{three}
\begin{thebibliography}{99}
\bibitem{Test} test reference
\end{thebibliography}
\end{document}

目录

在此处输入图片描述

第一章

在此处输入图片描述

第三章

在此处输入图片描述

参考

在此处输入图片描述

相关内容