How to filter out legend elements based on the mask ?

One may want to use the mask to highlight some part of a map. In which case, one may want to hide elements of the legend (in a composition) that are not displayed inside the polygon of the mask.

The in_mask function can be used for that purpose. Starting with QGIS 2.14, a new option has been added to the legend filter tool, allowing to specify an expression. A symbol will be part of the legend only if at least one of its features make the boolean expression evaluated to true.

That can especially be used to filter out features that are not visible inside the mask by using the function in_mask(). The first argument must be the EPSG code of the target CRS of the evaluated features. For example in_mask(2154) for a French Lambert 93.