Collin
Collin

Reputation: 264

MATLAB: Combining LaTeX characters with data formatting in axes title

I'm trying to figure out if there is a way for me to combine symbols via the TeX interpreter and data inputs into a single string for an axes title.

For example, my current code goes something like this:

figure
axHandle = axes;
appleTrees = 4;
s = sprintf( ...
    'Apples vs Acres\nNumber of Apples Trees $\alpha = %2.0f$', appleTrees);
title(axHandle,s,'Interpreter','LaTeX')

I understand this doesn't work, but I think it conveys what I am trying to do.

sprintf causes the following message:

Warning: Control Character '\l' is not valid. 
See 'doc sprintf' for control characters valid in the format string.

I can ditch sprintf and go simply quotes, but then I lose the data formatting / text formatting capability.

Upvotes: 2

Views: 815

Answers (1)

Matt
Matt

Reputation: 13943

The backslash \ is a special character when formatting strings for certain functions in Matlab. sprintf is one of those. To write a backslash, use \\ instead. Here is a list of other special characters and how they can be written.

In your case, use the following lines:

s = sprintf( ...
    'Apples vs Acres\nNumber of Apples Trees $\\alpha = %2.0f$', appleTrees);

Upvotes: 3

Related Questions