1

I am currently using matlab2tikz to export graphical data to latex.

Some of the plots I use contain both pgfplots objects such as \addplot and tikz objects such as \node or \draw in potentially large amounts.

I would like to be able to apply a global style modification to all \nodes of the graphical area of the axis, hence excluding all nodes related to legends, axis labels, ticks, ...

I tried some options but so far, I was not able to apply a style globally with the required result.

There might be an element of answer here, but I failed to transpose it to this case.

Here is a MWE showing the required result and some failing solutions.

\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}

Reference enter image description here

Desired result enter image description here

Non working solutions enter image description here enter image description here

EDIT I ended up using @marmot 's answer adding a scope inside of the axis environment. For matlab2tikz users that may need such approach, here is the matlab code used to modify the tex file generated by matlab2tikz. It allows to batch process the files instead of modifiying them manually.

% 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);

Hope this helps

  • Shouldn't it be \begin{axis}[/tikz/every node/.append style={inner sep=0pt}] ? – user121799 Oct 24 '18 at 17:53
2

In a axis, you are in the pgfplots directory, so you'd need to switch to tikz. If you don't want to mess up the axis labels, use a scope.

\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}

enter image description here

  • Thanks for your answer. I was afraid, there was no other option than to modify the content of the axis manually, that is why I did not consider a scope at first. All this is due to way matlab2tikz works. You can pass options to the axis within matlab, but AFAIK there is no direct way to provide a a wrapper inside the axis. Do you think something like execute at begin axis could allow to do this ? – BambOo Oct 24 '18 at 20:58
  • @BambOo I am not sure if an execute at begin axis alone will help, but maybe if you set these things with execute at end plot={\tikzset{...}} and unset them with execute at end axis={\tikzset{....}}, it could work. I did not try these things out. (But I guess that the axis labels are drawn at a very late stage, when pgfplots knows how large the plots are, yet I do not know for sure, so one has to try out. – user121799 Oct 24 '18 at 21:06
  • @BambOo I tried several combinations, none of them had the desired effect... :-( – user121799 Oct 24 '18 at 21:30
  • I finally chose the scope approach. As I said before, there is no direct way to add such wrapper in matlab2tikz options, so I wrote a small piece of matlab code that patches the original tex file. I updated my question to share this – BambOo Oct 25 '18 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.