编辑

编辑

我制作的(基于 tikz 的)图片通常是模块化的。因此,对图片的不同变体使用相同的源是理想的。

理想情况下,这将基于standalone,因为这是我已经在使用的,并提供一个定义条件块的新环境(我已conditionally在下面命名了该环境)。这些条件代码块可以通过选项启用includestandalone

main.tex

\documentclass{article}

\usepackage{standalone}
\usepackage{tikz}

\begin{document}
\includestandalone[enableall]{figure}

\includestandalone[enable=detour]{figure}

\includestandalone[enable={detour,longer-detour}]{figure}

\end{document}

figure.tex

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\pgfmathsetmacro{\myRadius}{2}

\coordinate (loop-start) at (-170:\myRadius);

\draw[
  ->,
  line width=7pt,
]
(loop-start)
arc (-170:170:\myRadius)
coordinate[pos=0.75] (detour-start)
coordinate[pos=0.25] (detour-end)
coordinate[pos=0.27] (detour-end-2)
;

\begin{conditionally}{detour}
  \draw[
    ->,
    line width=3pt,
    red,
  ] (detour-start) to[out=160, in=220, distance=6cm] (detour-end);
\end{conditionally}

\begin{conditionally}{longer-detour}
  \draw[
    ->,
    line width=3pt,
    red,
  ] (detour-start) to[out=45, in=-30, distance=10cm] (detour-end-2);
\end{conditionally}

\end{tikzpicture}
\end{document}

这样的事可能吗?

答案1

standalone该解决方案为包含的文件启用目标语法tikzpicture,但在主文件中使用自定义命令。

更具体地说,conditionally是一个采用单一、强制参数的环境。

  • 该参数应该由用于识别内容的简单标签/标记组成。
  • 在示例中,这用作 的一部分tikzpicture,但解决方案并不依赖于此。同样的机制同样可用于包含所选的引文或任意代码片段。

\flowconditional[<key-value list>]{<filename>}用于在主文档中按标签/标签选择片段<filename>

  • 目前仅定义了一个键,enable
  • enable采用逗号分隔的值列表。
  • <filename>每个值应为或中的标签/标记*,其中*表示应包含所有可用代码。
  • 然而,这一要求并非强制执行:如果传递给的值enable未在中定义filename,则该值将被默默忽略。

鉴于 OP 的图形文件名为\jobname-fig.tex,我们可以这样写

\flowconditionalpic[enable=*]{\jobname-fig}

\flowconditionalpic[enable=detour]{\jobname-fig}

\flowconditionalpic[enable={detour,longer-detour}]{\jobname-fig}

以产生目标行为。

目标输出

可以使用相同的框架来包含或不包含tabular环境(尽管包含行集需要更多工作)。例如,

\begin{table}
  \caption{Tables rather than pictures}
  \flowconditional[enable=tab1]{\jobname-tab}
  \flowconditional[enable=tab2]{\jobname-tab}
\end{table}

\begin{table}
  \caption{Order is irrelevant}
  \flowconditional[enable=*]{\jobname-tab}
  \flowconditional[enable={tab2,tab1}]{\jobname-tab}
\end{table}

其中\jobname-tab.tex包含

\documentclass{standalone}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\sffamily
\begin{conditionally}{tab1}
  \begin{tabular} {ll}
    \toprule
    \bfseries Column 1 & \bfseries Column 2 \\
    \midrule
    Row 1 & Cell A \\
    \bottomrule
  \end{tabular}
\end{conditionally}
\begin{conditionally}{tab2}
  \begin{tabular} {ll}
    \toprule
    \bfseries Column 1 & \bfseries Column 2 \\
    \midrule
    Row 1 & Cell A \\
    Row 2 & Cell B \\
    \bottomrule
  \end{tabular}
\end{conditionally}
\end{document}

与表格而不是 tikzpicture 的部分内容的想法相同

完整代码:

\begin{filecontents}[overwrite]{\jobname-fig.tex}
\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}  
  \pgfmathsetmacro{\myRadius}{2} 
  \coordinate (loop-start) at (-170:\myRadius);  
  \draw[->,line width=7pt,]
  (loop-start)
  arc (-170:170:\myRadius)
  coordinate[pos=0.75] (detour-start)
  coordinate[pos=0.25] (detour-end)
  coordinate[pos=0.27] (detour-end-2)
  ;  
  \begin{conditionally}{detour}
    \draw[
    ->,
    line width=3pt,
    red,
    ] (detour-start) to[out=160, in=220, distance=6cm] (detour-end);
  \end{conditionally}  
  \begin{conditionally}{longer-detour}
    \draw[
    ->,
    line width=3pt,
    red,
    ] (detour-start) to[out=45, in=-30, distance=10cm] (detour-end-2);
  \end{conditionally}
\end{tikzpicture}
\end{document}
\end{filecontents}
\begin{filecontents}[overwrite]{\jobname-tab.tex}
\documentclass{standalone}
\usepackage{array}
\usepackage{booktabs}
\begin{document}
\sffamily
\begin{conditionally}{tab1}
  \begin{tabular} {ll}
    \toprule
    \bfseries Column 1 & \bfseries Column 2 \\
    \midrule
    Row 1 & Cell A \\
    \bottomrule
  \end{tabular}
\end{conditionally}
\begin{conditionally}{tab2}
  \begin{tabular} {ll}
    \toprule
    \bfseries Column 1 & \bfseries Column 2 \\
    \midrule
    Row 1 & Cell A \\
    Row 2 & Cell B \\
    \bottomrule
  \end{tabular}
\end{conditionally}
\end{document}
\end{filecontents}

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/705487/ addaswyd o gwestiwn Flow: https://tex.stackexchange.com/q/705458/
\usepackage[subpreambles]{standalone}
\usepackage{tikz}
\ExplSyntaxOn
\bool_new:N \l_flow_condition_bool
\bool_set_false:N \l_flow_condition_bool
\keys_define:nn { flow }
{
  enable .clist_set:N = \l_flow_conditions_clist,
  enable .initial:n = ,
  enable .default:n = ,
}
\NewDocumentEnvironment { conditionally } { m +b }
{
  \clist_if_in:NnT \l_flow_conditions_clist { #1 }
  { \bool_set_true:N \l_flow_condition_bool } 
  \clist_if_in:NnT \l_flow_conditions_clist { * }
  { \bool_set_true:N \l_flow_condition_bool } 
  \bool_if:NT \l_flow_condition_bool { #2 }
}{  }
\NewDocumentCommand \flowconditional { O {} m }
{
  \group_begin:
    \keys_set:nn { flow } { #1 }
    \includestandalone { #2 }
  \group_end:
}
\ExplSyntaxOff

\begin{document}
\flowconditional[enable=*]{\jobname-fig}

\flowconditional[enable=detour]{\jobname-fig}

\flowconditional[enable={detour,longer-detour}]{\jobname-fig}


\begin{table}
  \caption{Tables rather than pictures}
  \flowconditional[enable=tab1]{\jobname-tab}
  \flowconditional[enable=tab2]{\jobname-tab}
\end{table}

\begin{table}
  \caption{Order is irrelevant}
  \flowconditional[enable=*]{\jobname-tab}
  \flowconditional[enable={tab2,tab1}]{\jobname-tab}
\end{table}

\end{document}

编辑

下面添加了一个额外的键standalone options。最初,它被设置为mode = tex。使用没有值的键将清空选项。使用有值的键将把选项设置为该值。在下面的示例中,初始值适用于大多数情况,用于angle=90演示更改选项的效果。

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/705487/ addaswyd o gwestiwn Flow: https://tex.stackexchange.com/q/705458/
\usepackage[subpreambles]{standalone}
\usepackage{tikz}
\ExplSyntaxOn
\bool_new:N \l_flow_condition_bool
\bool_set_false:N \l_flow_condition_bool
\keys_define:nn { flow }
{
  enable .clist_set:N = \l_flow_conditions_clist,
  enable .initial:n = ,
  enable .default:n = ,
  standalone ~ options .tl_set:N = \l_flow_standalone_tl,
  standalone ~ options .default:n = ,
  standalone ~ options .initial:n = {mode = tex},
}
\NewDocumentEnvironment { conditionally } { m +b }
{
  \clist_if_in:NnT \l_flow_conditions_clist { #1 }
  { \bool_set_true:N \l_flow_condition_bool } 
  \clist_if_in:NnT \l_flow_conditions_clist { * }
  { \bool_set_true:N \l_flow_condition_bool } 
  \bool_if:NT \l_flow_condition_bool { #2 }
}{  }
\NewDocumentCommand \flowconditional { O {} m }
{
  \group_begin:
    \keys_set:nn { flow } { #1 }
    \flow_includestandalone:Vn \l_flow_standalone_tl { #2 }
  \group_end:
}
\cs_new_protected:Nn \flow_includestandalone:nn
{
  \includestandalone [ #1 ] { #2 }
}
\cs_generate_variant:Nn \flow_includestandalone:nn { Vn }
\ExplSyntaxOff

\begin{document}
\flowconditional[enable=*]{\jobname-fig}

\flowconditional[enable=detour,standalone options={angle=90}]{\jobname-fig}

\flowconditional[enable={detour,longer-detour}]{\jobname-fig}


\begin{table}
  \caption{Tables rather than pictures}
  \flowconditional[enable=tab1]{\jobname-tab}
  \flowconditional[enable=tab2]{\jobname-tab}
\end{table}

\begin{table}
  \caption{Order is irrelevant}
  \flowconditional[enable=*]{\jobname-tab}
  \flowconditional[enable={tab2,tab1}]{\jobname-tab}
\end{table}

\end{document}

设置“独立选项={角度=90}”的效果

答案2

您可以使用\pic

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\tikzset{
    pics/conditional loop/.style={
        code={
            \tikzset{conditional loop/.cd, #1}
            \coordinate
                (-loop-start)
                at (-170:{\pgfkeysvalueof{/tikz/conditional loop/radius}})
            ;
            \draw[
              ->,
              line width=7pt,
            ]
                (-loop-start)
                arc[
                    start angle=-170, 
                    end angle=170, 
                    radius={\pgfkeysvalueof{/tikz/conditional loop/radius}}
                ]
                coordinate[pos=0.75] (-detour-start)
                coordinate[pos=0.25] (-detour-end-short)
                coordinate[pos=0.27] (-detour-end-long)
            ;
            \foreach \o/\i/\d/\e in \conditionallooploopsettings {
                \draw[
                    ->,
                    line width=3pt,
                    red,
                ] 
                    (-detour-start) 
                    to[out=\o, in=\i, distance=\d] 
                    (-\e)
                ;
            }
        }
    },
    conditional loop/radius/.initial={2},
    conditional loop/loop settings/.store in=\conditionallooploopsettings,
    conditional loop/loop settings={},
    conditional loop/all/.style={
        loop settings={160/220/6cm/detour-end-short,45/-30/10cm/detour-end-long}
    },
    conditional loop/detour/.style={
        loop settings={160/220/6cm/detour-end-short}
    },
    conditional loop/longer detour/.style={
        loop settings={45/-30/10cm/detour-end-long}
    }
}

\begin{tikzpicture}

\pic at (0,0) {conditional loop};

\pic at (0,-10) {conditional loop={all}};

\pic at (0,-20) {conditional loop={detour}};

\pic at (0,-30) {conditional loop={longer detour}};

\pic at (0,-40) {conditional loop={radius=1cm, all}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

此变体还允许conditional loop={detour, longer detour}

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\tikzset{
    pics/conditional loop/.style={
        code={
            \tikzset{conditional loop/.cd, #1}
            \coordinate
                (-loop-start)
                at (-170:{\pgfkeysvalueof{/tikz/conditional loop/radius}})
            ;
            \draw[
              ->,
              line width=7pt,
            ]
                (-loop-start)
                arc[
                    start angle=-170, 
                    end angle=170, 
                    radius={\pgfkeysvalueof{/tikz/conditional loop/radius}}
                ]
                coordinate[pos=0.75] (-detour-start)
                coordinate[pos=0.25] (-detour-end-short)
                coordinate[pos=0.27] (-detour-end-long)
            ;
            \pgfkeys{/tikz/conditional loop/draw loops}
        }
    },
    conditional loop/detour arrow/.style={
        ->,
        line width=3pt,
        red,
    },
    conditional loop/radius/.initial={2},
    conditional loop/loop settings/.store in=\conditionallooploopsettings,
    conditional loop/draw loops/.initial={},
    conditional loop/detour/.style={
        draw loops/.append code={
            \draw[conditional loop/detour arrow]
                (-detour-start) 
                to[out=160, in=220, distance=6cm] 
                (-detour-end-short);
        }
    },
    conditional loop/longer detour/.style={
        draw loops/.append code={
            \draw[conditional loop/detour arrow]
                (-detour-start) 
                to[out=34, in=-30, distance=10cm] 
                (-detour-end-long);
        }
    },
    conditional loop/all/.style={
        detour,
        longer detour
    }
}

\begin{tikzpicture}

\pic at (0,0) {conditional loop};

\pic at (0,-10) {conditional loop={all}};

\pic at (0,-20) {conditional loop={detour}};

\pic at (0,-30) {conditional loop={longer detour}};

\pic at (0,-40) {conditional loop={detour, longer detour}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容