Skip to content

Exporting plots with sgtitle() to transparent PDF crops title #406

@TCWORLD

Description

@TCWORLD

The logic that detects block text on the periphery of the image to determine the cropping bounds doesn't yet handle text elements applied by the sgtitle function. As a result the cropping of the bounding box trims off this title.

The function adds text to the figure, but with a type of 'subplottext' when using findobj(). This text is therefore not detected by the current code. To fix this, I made two changes.

First in this section:

export_fig/export_fig.m

Lines 1140 to 1143 in 24c901e

hTitle = fixBlackText(hAxes,'Title');
try hCBs = getappdata(hAxes,'LayoutPeers'); catch, hCBs=[]; end %issue #383
hCBs = unique([findall(fig,'tag','Colorbar'), hCBs]);
hCbTxt = fixBlackText(hCBs,'Title'); % issue #382

Add:

hSpTxt = findobj(fig, 'Type', 'subplottext');
hSpTxt = fixBlackText(hSpTxt,'');

Along with the corresponding restoring colour a little later on:

set(hSpTxt,'Color',[0,0,0]);

Secondly, in this section:

export_fig/export_fig.m

Lines 2597 to 2602 in 24c901e

function hText = fixBlackText(hObject, propName)
try
hText = get(hObject, propName);
try hText = [hText{:}]; catch, end %issue #383
for idx = numel(hText) : -1 : 1
hThisText = hText(idx);

Change:

hText = get(hObject, propName);

To:

if isempty(propName)
    hText = hObject;
else
    hText = get(hObject, propName);
end

This allows the subplottext objects to be detected and recoloured so the cropping doesn't remove them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions