我目前正在使用matlab2tikz
将图形数据导出到latex
。
我使用的某些图表既包含pgfplots
诸如的对象\addplot
,也包含tikz
诸如\node
或的对象\draw
,并且其数量可能很大。
我希望能够对\node
图形区域的所有区域应用全局样式修改axis
,从而排除与图例、轴标签、刻度等相关的所有节点...
我尝试了一些选项,但到目前为止,我还无法全局应用具有所需结果的样式。
也许有答案这里,但我没能将其转换到这个案例中。
这是 MWE,显示了所需的结果和一些失败的解决方案。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
% Reference
\begin{axis}
\addplot[domain=0:10] {x};
\node[fill=orange,draw=black] at (axis cs: (5,5) {Hello World};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% Desired result
\begin{axis}
\addplot[domain=0:10] {x};
\node[fill=orange,draw=black,inner sep=0pt] at (axis cs: (5,5) {Hello World};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% Non working solution 1 : nothing seems to happen
\begin{axis}[every node/.append style={inner sep=0pt}]
\addplot[domain=0:10] {x};
\node[fill=orange,draw=black] at (axis cs: (5,5) {Hello World};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% Non working solution 2 : ticks are also modified
\tikzset{every node/.append style={inner sep=0pt}}
\begin{axis}
\addplot[domain=0:10] {x};
\node[fill=orange,draw=black] at (axis cs: (5,5) {Hello World};
\end{axis}
\end{tikzpicture}
\end{document}
编辑我最终使用了 @marmot 的答案,在环境中添加了一个范围axis
。对于matlab2tikz
可能需要这种方法的用户,这里是matlab
用于修改tex
生成的文件的代码matlab2tikz
。它允许批量处理文件,而不是手动修改它们。
% Load tex file in workspace as cell
texfile = importdata(texfilepath);
% Find begin and end of axis content
beginaxisline = find(cellfun(@(x) strcmp(x,']')==1,texfile));
endaxisline = find(cellfun(@(x) strcmp(x,'\end{axis}')==1,texfile));
% Determine modifications to be added depending of figure contents
% Lines to be added after \begin{axis}[...] stored as the lines of a cell
beginaxismod = {...;...;...};
% Lines to be added before \end{axis} stored as the lines of a cell
endaxismod = {...;...;...};
% Assembly of the new tex file contents
texfilemod = [texfile(1:beginaxisline,1) ; ...
beginaxismod; ...
texfile((beginaxisline+1):(endaxisline-1),1);...
endaxismod; ...
texfile(endaxisline:end,1)...
];
% Overwrite modified .tex file
fid = fopen(texfilepath,'rt+');
fprintf(fid,'%s\n', texfilemod{:});
fclose(fid);
希望这可以帮助
答案1
在轴中,您位于pgfplots
目录中,因此您需要切换到tikz
。如果您不想弄乱轴标签,请使用范围。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
% Now working solution 1 : nothing seems to happen
\begin{axis}[/tikz/every node/.append style={inner sep=0pt}]
\addplot[domain=0:10] {x};
\node[fill=orange,draw=black] at (axis cs: (5,5) {Hello World};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% Now working solution 2 : nothing seems to happen
\begin{axis}
\addplot[domain=0:10] {x};
\begin{scope}[every node/.append style={inner sep=0pt}]
\node[fill=orange,draw=black] at (axis cs: (5,5) {Hello World};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}