将所有标题行居中,使其长度与第一行相同

将所有标题行居中,使其长度与第一行相同

请参阅下面我的代码

\noindent\makebox[\textwidth]{%
\tikzstyle{every node}=[circle, draw, fill=black!100,
                       inner sep=0pt, minimum width=5pt]
\begin{tikzpicture}[thick,scale=0.8]%
%1st column
\draw(0,0)node[label={[yshift=-0.4cm, xshift=0.1cm]90:{}}](1){};
\draw(0,5)node[label={[yshift=0.4cm, xshift=0.1cm]270:{}}](2){};
\draw(5,5)node[label={[yshift=-0.4cm, xshift=-0.1cm]90:{}}](3){};
\draw(5,0)node[label={[yshift=-0.9cm, xshift=-0.9cm]:{}}](4){};

%1st column
\draw(1)--(2);
\draw(2)--(3);
\draw(3)--(4);
\draw(4)--(1);
\end{tikzpicture}
}\captionsetup{justification=centering}
{\captionof{figure}{This figure shows a cycle of length $4$.
\\Note: I need to make a note here. Thus, this is a very very very long caption. However, it looks weird being center-justified as the sentences differ in length especiallyWhenThereAreLongWords like this. How can I make it center-justified and all lines have an equal length with the first line.}}

我附上了当前输出。我们可以像所需的标题 1 或 2 那样对其进行编码吗?谢谢!!

电流输出

所需标题1

所需标题2

答案1

像这样:

在此处输入图片描述

(红线表示文本区域左右边界)

对于这样的标题和节点形成,您需要测量标题宽度,然后设置标题宽度:

\documentclass{article}
\usepackage{caption}  % new
\newsavebox\capbox
\newlength\capwidth
\usepackage{tikz}     % new
\usetikzlibrary{shapes.geometric}   % new

%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}[
box/.style = {regular polygon, regular polygon sides=4,
              draw, minimum size=5cm},
dot/.style = {circle, fill, 
              inner sep=0pt, minimum size=4pt}
                    ]
\node (box) [box]   {};
\foreach \i in {1,2,3,4} 
    \node[dot] at (box.corner \i) {};
\end{tikzpicture}
    \savebox\capbox{This figure shows a cycle of length $4$.}
    \setlength\capwidth{\dimexpr\wd\capbox+4em}
    \captionsetup{width=\capwidth}
\caption{\usebox{\capbox}}
\caption*{Note: I need to make a note here. Thus, this is a very very very long caption. However, it looks weird being center-justified as the sentences differ in length especiallyWhenThereAreLongWords like this. How can I make it center-justified and all lines have an equal length with the first line.}
    \end{figure}
\end{document}

正如您所看到的,我改变了绘制正方形的方式。

附录(1):

我无法弄清楚您的“发送”示例中的问题是什么。从显示的图像可以看出,您不需要做任何特殊的事情:

\documentclass{article}
\usepackage{caption}  
\usepackage{tikz}     
\usetikzlibrary{shapes.geometric}   % new

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}[
box/.style = {regular polygon, regular polygon sides=4,
              draw, minimum size=5cm},
dot/.style = {circle, fill,
              inner sep=0pt, minimum size=4pt}
                    ]
\node (box) [box]   {};
\foreach \i in {1,2,3,4}
    \node[dot] at (box.corner \i) {};
\end{tikzpicture}
\caption{This figure shows a cycle of length $4$.}
\caption*{Note: I need to make a note here. Thus, this is a very very very long caption. However, it looks weird being center-justified as the sentences differ in length especiallyWhenThereAreLongWords like this. How can I make it center-justified and all lines have an equal length with the first line.}
    \end{figure}
\end{document}

在此处输入图片描述

附录(2):

我猜现在你喜欢在单行标题的情况下将标题居中,但在多行情况下应该只将标题中的最后一行文本居中。如果我没记错的话,那么在caption包的序言加载中,就像上面两个示例一样:

\usepackage{microtype}
\usepackage[justification=centerlast]{caption}

比起以下的 MWE,现在两种情况的所需字幕格式都是:

\documentclass{article}
\usepackage{microtype}  % new
\usepackage[justification=centerlast]{caption}  % Changed
\newsavebox\capbox
\newlength\capwidth
\usepackage{tikz}     
\usetikzlibrary{shapes.geometric}  

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}[
box/.style = {regular polygon, regular polygon sides=4,
              draw, minimum size=5cm},
dot/.style = {circle, fill,
              inner sep=0pt, minimum size=4pt}
                    ]
\node (box) [box]   {};
\foreach \i in {1,2,3,4}
    \node[dot] at (box.corner \i) {};
\end{tikzpicture}
    \savebox\capbox{This figure shows a cycle of length $4$.}
    \setlength\capwidth{\dimexpr\wd\capbox+4em}
    \captionsetup{width=\capwidth}
\caption{\usebox{\capbox}}
\caption*{Note: I need to make a note here. Thus, this is a very very very long caption. However, it looks weird being center-justified as the sentences differ in length especiallyWhenThereAreLongWords like this. How can I make it center-justified and all lines have an equal length with the first line.}
    \end{figure}
   \begin{figure}
    \centering
\begin{tikzpicture}[
box/.style = {regular polygon, regular polygon sides=4,
              draw, minimum size=5cm},
dot/.style = {circle, fill,
              inner sep=0pt, minimum size=4pt}
                    ]
\node (box) [box]   {};
\foreach \i in {1,2,3,4}
    \node[dot] at (box.corner \i) {};
\end{tikzpicture}
\caption{This figure shows a cycle of length $4$.}
\caption*{Note: I need to make a note here. Thus, this is a very very very long caption. However, it looks weird being center-justified as the sentences differ in length especiallyWhenThereAreLongWords like this. How can I make it center-justified and all lines have an equal length with the first line.}
    \end{figure}
\end{document}

在此处输入图片描述

答案2

对于您第二喜欢的输出,您只需添加带星号的标题和 raggedright 即可,如下所示:

\caption*{\raggedright Note: Really long note...}

相关内容