自动报告打印机:处理目录中的长技术表达和图例条目

自动报告打印机:处理目录中的长技术表达和图例条目

我目前正在用 Latex 编写一个自动打印机程序,用于打印一些技术报告。整个打印机可以通过外部程序控制和启动,该程序提供 csv 文件和数据,使用 Pgfplots 处理后生成报告或 Beamer 格式的不同标准化图表。但我的问题是自动生成的文件名太长。由于该报告用于技术测量,因此文件名或图例条目可以标记为 File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239 或类似名称。这给我带来了几个问题,因为 Latex 无法在目录和 pgfplots 图例条目中破坏这些名称。

有什么方法可以强制 Latex 将这些长标签拆分成多行?遗憾的是,代码有点长,无法提供完整的工作示例,但无论如何,问题应该(从我的理解)与 plotfile 函数无关。Adjustbox 用于压缩 Report 或 Beamer 格式的大 pgfplotstable 图。

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{ifthen} 
\usepackage[export]{adjustbox}
\usepackage{underscore}
\usepackage{graphicx, siunitx}  
\usepackage{filecontents}
\usepackage{tikz, pgfplots, pgfplotstable}
\usepgfplotslibrary{units} 
\pgfplotsset{compat=newest}

% ======== Commands ========
\newcommand{\comma}{/pgf/number format/read comma as period}
\newcommand{\semicolon}{semicolon}
\newcommand{\commadelim}{comma}
\newcommand{\type}{commadelim}

% ======== Plotfile ========
\newcommand{\plotfile}[3]{
    \edef\rememberuscatcode{% make the underscore into a printable symbol,
        \catcode`_=\the\catcode`_   
        \let\noexpand\rememberuscatcode\relax
    }
    \catcode`_=12           % but first remember its meaning
    %
    % Write correct column seperator
    \newcommand{\tempmacro}{#3} 
    \ifx\tempmacro\semicolon
        \renewcommand{\type}{semicolon}
    \fi 
    \ifx\tempmacro\commadelim
        \renewcommand{\type}{comma}
    \fi  
    \pgfplotstableread[col sep=\type]
    {\detokenize{#1}}{\table} 
    \pgfplotstablegetcolsof{\table}
    \renewcommand{\tempmacro}{#2}
    \pgfmathtruncatemacro\numberofcols{%Define max. Nr of used columns  
        \ifx\tempmacro\undefined 
            {\pgfplotsretval-1}
        \else
            {#2}
        \fi         
    }%
    \pgfplotsinvokeforeach{1,...,\numberofcols}{
        \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
        \addplot table [y index=##1, \comma] {\table};
        \addlegendentryexpanded{\colname}
    }%
    \rememberuscatcode
}%

\begin{document}
    \tableofcontents

    \begin{filecontents}    {File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}  
        Entry1;Entry_With_Different_Settings;Entry2_With_Different_Settings
        0,25;0;0
        4;75;76
        9;70;70
    \end{filecontents}

    \section{Print}
    \subsection{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239}
        \begin{figure}[ht]
            \begin{center}
                \begin{adjustbox}{minipage=\textwidth, pagecenter, 
                    max totalheight = 0.74\textheight, max width = 0.9\textwidth}
                    \centering
                    \begin{tikzpicture}
                        \begin{axis}[
                            width=0.82\linewidth, height=0.48\linewidth, 
                            grid=major, grid style={dashed,gray!30},
                            xlabel=X Axis, 
                            x unit=\si{\volt \per \meter},
                            xmin = 0, xmax = 9,
                            ylabel=Y Axis,  y unit=\si{\milli \ampere},
                            legend pos=outer north east, legend cell align={left},      
                        ]
                        \addplot[mark=none, red, dashed] coordinates {(0,100) (10,100)};                 
                        \addplot[mark=none, red, dashed,forget plot] coordinates {(0,30) (10,25)};      
                        \addlegendentry{\footnotesize Limit}
                        %
                        \plotfile{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}{2}{semicolon}
                        %
                        \end{axis}
                    \end{tikzpicture}           
                \end{adjustbox}
            \end{center}
        \end{figure}
\end{document}

在此处输入图片描述

答案1

此版本使用 url 包。大多数情况下,您只需要使用\protect\url它来避免过早扩展它。我还使用图例来限制文本宽度,并在条目之间添加了间隙。

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{ifthen} 
\usepackage[export]{adjustbox}
\usepackage{underscore}
\usepackage{graphicx, siunitx}  
\usepackage{filecontents}
\usepackage{tikz, pgfplots, pgfplotstable}
\usepgfplotslibrary{units} 
\pgfplotsset{compat=newest}
\usepackage{url}

% ======== Commands ========
\newcommand{\comma}{/pgf/number format/read comma as period}
\newcommand{\semicolon}{semicolon}
\newcommand{\commadelim}{comma}
\newcommand{\type}{commadelim}

% ======== Plotfile ========
\newcommand{\plotfile}[3]{
    \edef\rememberuscatcode{% make the underscore into a printable symbol,
        \catcode`_=\the\catcode`_   
        \let\noexpand\rememberuscatcode\relax
    }
    \catcode`_=12           % but first remember its meaning
    %
    % Write correct column seperator
    \newcommand{\tempmacro}{#3} 
    \ifx\tempmacro\semicolon
        \renewcommand{\type}{semicolon}
    \fi 
    \ifx\tempmacro\commadelim
        \renewcommand{\type}{comma}
    \fi  
    \pgfplotstableread[col sep=\type]
    {\detokenize{#1}}{\table} 
    \pgfplotstablegetcolsof{\table}
    \renewcommand{\tempmacro}{#2}
    \pgfmathtruncatemacro\numberofcols{%Define max. Nr of used columns  
        \ifx\tempmacro\undefined 
            {\pgfplotsretval-1}
        \else
            {#2}
        \fi         
    }%
    \pgfplotsinvokeforeach{1,...,\numberofcols}{
        \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
        \addplot table [y index=##1, \comma] {\table};
        \addlegendentryexpanded[text width=1in, text depth=]{\protect\url{\colname}}
        \addlegendimage{empty legend}
        \addlegendentry{}
    }%
    \rememberuscatcode
}%

\begin{document}
    {\sloppy\tableofcontents}

    \begin{filecontents}    {File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}  
        Entry1;Entry_With_Different_Settings;Entry2_With_Different_Settings
        0,25;0;0
        4;75;76
        9;70;70
    \end{filecontents}

    \section{Print}
    \subsection{\protect\url{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239}}
    
        \begin{figure}[ht]
            \begin{center}
                \begin{adjustbox}{minipage=\textwidth, pagecenter, 
                    max totalheight = 0.74\textheight, max width = 0.9\textwidth}
                    \centering
                    \begin{tikzpicture}
                        \begin{axis}[
                            width=0.82\linewidth, height=0.48\linewidth, 
                            grid=major, grid style={dashed,gray!30},
                            xlabel=X Axis, 
                            x unit=\si{\volt \per \meter},
                            xmin = 0, xmax = 9,
                            ylabel=Y Axis,  y unit=\si{\milli \ampere},
                            legend pos=outer north east, legend cell align={left},      
                        ]
                        \addplot[mark=none, red, dashed] coordinates {(0,100) (10,100)};                 
                        \addplot[mark=none, red, dashed,forget plot] coordinates {(0,30) (10,25)};      
                        \addlegendentry{\footnotesize Limit}
                        %
                        \plotfile{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}{2}{semicolon}
                        %
                        \end{axis}
                    \end{tikzpicture}           
                \end{adjustbox}
            \end{center}
        \end{figure}
\end{document}

这将执行测试以确定列名是否长于 1 英寸。为了使其正常工作,我不得不将大部分 pgfplotstable 代码移到 tikzpicture 之外。

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{ifthen} 
\usepackage[export]{adjustbox}
\usepackage{underscore}
\usepackage{graphicx, siunitx}  
\usepackage{filecontents}
\usepackage{tikz, pgfplots, pgfplotstable}
\usepgfplotslibrary{units} 
\pgfplotsset{compat=newest}
\usepackage{url}

\newsavebox{\urlbox}
\newif\iflongcolnames

% ======== Commands ========
\newcommand{\comma}{/pgf/number format/read comma as period}
\newcommand{\semicolon}{semicolon}
\newcommand{\commadelim}{comma}
\newcommand{\type}{commadelim}

% ======== Plotfile ========
\newcommand{\plotfile}{
    \pgfplotsinvokeforeach {1,...,\numberofcols}{
        \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
        \addplot table [y index=##1, \comma] {\table};
        \iflongcolnames
          \addlegendentryexpanded[text width=1in, text depth=]{\protect\url{\colname}}
          \addlegendimage{empty legend}
          \addlegendentry{}
        \else
          \addlegendentryexpanded{\colname}
        \fi
    }%
}%
% ====== preview ++++++
\newcommand{\preview}[3]{%
    \edef\rememberuscatcode{% make the underscore into a printable symbol,
        \catcode`_=\the\catcode`_   
        \let\noexpand\rememberuscatcode\relax
    }
    \catcode`_=12           % but first remember its meaning
    %
    % Write correct column seperator
    \newcommand{\tempmacro}{#3}%
    \ifx\tempmacro\semicolon
        \renewcommand{\type}{semicolon}%
    \fi 
    \ifx\tempmacro\commadelim
        \renewcommand{\type}{comma}%
    \fi  
    \pgfplotstableread[col sep=\type]%
    {\detokenize{#1}}{\table} %
    \pgfplotstablegetcolsof{\table}%
    \renewcommand{\tempmacro}{#2}%
    \pgfmathtruncatemacro\numberofcols{%Define max. Nr of used columns  
        \ifx\tempmacro\undefined 
            0
        \else
            {#2}
        \fi         
    }%
    \foreach \i in {1,...,\numberofcols}{%
        \pgfplotstablegetcolumnnamebyindex{\i}\of{\table}\to{\colname}%
        \savebox{\urlbox}{\colname}%
        \ifdim\wd\urlbox > 1in\relax
          \global\longcolnamestrue
        \fi
    }%
    \rememberuscatcode
}%
\begin{document}
    {\sloppy\tableofcontents}

    \begin{filecontents}    {File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}  
        Entry1;Entry_With_Different_Settings;Entry2_With_Different_Settings
        0,25;0;0
        4;75;76
        9;70;70
    \end{filecontents}

    \section{Print}
    \subsection{\protect\url{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239}}
    
        \begin{figure}[ht]
            \preview{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}{2}{semicolon}
                \begin{adjustbox}{minipage=\textwidth, pagecenter, 
                    max totalheight = 0.74\textheight, max width = 0.9\textwidth}
                    \centering
                    \begin{tikzpicture}
                        \begin{axis}[
                            width=0.82\linewidth, height=0.48\linewidth, 
                            grid=major, grid style={dashed,gray!30},
                            xlabel=X Axis, 
                            x unit=\si{\volt \per \meter},
                            xmin = 0, xmax = 9,
                            ylabel=Y Axis,  y unit=\si{\milli \ampere},
                            legend pos=outer north east, legend cell align={left},      
                        ]
                        \addplot[mark=none, red, dashed] coordinates {(0,100) (10,100)};                 
                        \addplot[mark=none, red, dashed,forget plot] coordinates {(0,30) (10,25)};      
                        \addlegendentry{\footnotesize Limit}
                        %
                        \plotfile
                        %
                        \end{axis}
                    \end{tikzpicture}           
                \end{adjustbox}
        \end{figure}
\end{document}

答案2

问题实际上有两个方面:章节中的长标题和长的图例宽度。

为了解决分段问题,我设置了一个宏\breakabledef,它将创建一个\def,但下划线将被替换为,\_\allowbreak插入到每个非空格/非宏字符之后。然后,只需使用这个定义的宏作为节标题即可。

至于剧情图例,我不太了解如何tikz改变图例位置或剧情的换行。但我会注意到环境的组合center与 相冲突adjustbox,所以我在那里删除/更改了一些东西,让它回到中心。

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{ifthen} 
\usepackage[export]{adjustbox}
\usepackage{underscore}
\usepackage{graphicx, siunitx}  
\usepackage{filecontents}
\usepackage{tikz, pgfplots, pgfplotstable}
\usepgfplotslibrary{units} 
\pgfplotsset{compat=newest}

% ======== Commands ========
\newcommand{\comma}{/pgf/number format/read comma as period}
\newcommand{\semicolon}{semicolon}
\newcommand{\commadelim}{comma}
\newcommand{\type}{commadelim}

% ======== Plotfile ========
\newcommand{\plotfile}[3]{
    \edef\rememberuscatcode{% make the underscore into a printable symbol,
        \catcode`_=\the\catcode`_   
        \let\noexpand\rememberuscatcode\relax
    }
    \catcode`_=12           % but first remember its meaning
    %
    % Write correct column seperator
    \newcommand{\tempmacro}{#3} 
    \ifx\tempmacro\semicolon
        \renewcommand{\type}{semicolon}
    \fi 
    \ifx\tempmacro\commadelim
        \renewcommand{\type}{comma}
    \fi  
    \pgfplotstableread[col sep=\type]
    {\detokenize{#1}}{\table} 
    \pgfplotstablegetcolsof{\table}
    \renewcommand{\tempmacro}{#2}
    \pgfmathtruncatemacro\numberofcols{%Define max. Nr of used columns  
        \ifx\tempmacro\undefined 
            {\pgfplotsretval-1}
        \else
            {#2}
        \fi         
    }%
    \pgfplotsinvokeforeach{1,...,\numberofcols}{
        \pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
        \addplot table [y index=##1, \comma] {\table};
        \addlegendentryexpanded{\colname}
    }%
    \rememberuscatcode
}%

\usepackage{tokcycle}

\newcommand\breakabledef[2]{%
  \resettokcycle
  \Characterdirective{\ifx_##1\addcytoks{\_}\else\addcytoks{##1}\fi
    \addcytoks{\allowbreak}}%
  \tokcyclexpress{#2}%
  \expandafter\def\expandafter#1\expandafter{\the\cytoks}%
}

\begin{document}
\breakabledef\mycsv
  {File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239}

    \tableofcontents

    \begin{filecontents}[overwrite]    {File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}  
        Entry1;Entry_With_Different_Settings;Entry2_With_Different_Settings
        0,25;0;0
        4;75;76
        9;70;70
    \end{filecontents}

    \section{Print}
    \subsection{\mycsv}
        \begin{figure}[ht]
%            \begin{center}
                \begin{adjustbox}{minipage=.9\textwidth, pagecenter, 
                    max totalheight = 0.74\textheight, max width = 0.8\textwidth}
                    \centering
                    \begin{tikzpicture}
                        \begin{axis}[
                            width=0.82\linewidth, height=0.48\linewidth, 
                            grid=major, grid style={dashed,gray!30},
                            xlabel=X Axis, 
                            x unit=\si{\volt \per \meter},
                            xmin = 0, xmax = 9,
                            ylabel=Y Axis,  y unit=\si{\milli \ampere},
                           legend pos=outer north east, 
                            legend cell align={left},      
                        ]
                        \addplot[mark=none, red, dashed] coordinates {(0,100) (10,100)};                 
                        \addplot[mark=none, red, dashed,forget plot] coordinates {(0,30) (10,25)};      
                        \addlegendentry{\footnotesize Limit}
                        %
                        \plotfile{File_Testname_3V6_1V7_Remeasure_Operator1_Operator2_D20210512T102239.csv}{2}{semicolon}
                        %
                        \end{axis}
                    \end{tikzpicture}           
                \end{adjustbox}
%            \end{center}
        \end{figure}
\end{document}

在此处输入图片描述

相关内容